Re: [PHP] Oddity

2002-02-04 Thread Mike Maltese

This could be the problem in your for loop: $elementKey = key(
$macroDataArray ) - you're using the = assignment operator. Try == or =.

Mike

- Original Message -
From: Chris Boget [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Monday, February 04, 2002 8:29 AM
Subject: [PHP] Oddity


 Ok, could someone tell me why this isn't working?
 (note, I took out all my error checks.  This is just the
 relevant code...

 $query = Query that does return row(s);
 $result = mysql( $dbname, $query );

 $macroDataArray = mysql_fetch_array( $result );

 for( reset( $macroDataArray ); $elementKey = key( $macroDataArray );
next( $macroDataArray )) {
 echo Value for key $elementKey = $macroDataArray[$elementKey]br\n;

 }
 // this should spit out one line for every element.

 That doesn't work but this does:

 foreach( $macroDataArray as $elementKey = $elementValue ) {
 echo Value for key $elementKey = $elementValuebr\n;

 }
 // this spits out 2 lines for every element.

 Why?  What's going on?  I could use the second bit of code, but that
returns
 four pairs for each array element:

 ELEMENT   VALUE
 05000
 KeyName  5000

 for example, where the above is the same element.  I really don't need the
 numerical key (the 0 above), just the name and that's why I need to find
 out why the first FOR loop isn't working.

 Any ideas?  I've tried everything... :(

 Chris




 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Oddity

2002-02-04 Thread Chris Boget

 $macroDataArray = mysql_fetch_array( $result );

I'm still curious what is going wrong, but I've found a work arround.
One of the things I love about PHP is that you learn something new
just about every day.  Instead of fetch_array(), I can use fetch_assoc()
and it'll do just what I need. :)

Chris



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Oddity

2002-02-04 Thread Mike Maltese

You may want to give mysql_fetch_object a try. I like it because you end up
typing less. You access elements like this: $row-data, instead of
$row['data']. Just my $0.02.

Mike
- Original Message -
From: Chris Boget [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Monday, February 04, 2002 8:42 AM
Subject: Re: [PHP] Oddity


  $macroDataArray = mysql_fetch_array( $result );

 I'm still curious what is going wrong, but I've found a work arround.
 One of the things I love about PHP is that you learn something new
 just about every day.  Instead of fetch_array(), I can use fetch_assoc()
 and it'll do just what I need. :)

 Chris



 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




RE: [PHP] Oddity

2002-02-04 Thread Andrew Chase

You can also do

?

$macroDataArray = mysql_fetch_array($result,MYSQL_ASSOC)

foreach(array_keys($macroDataArray) as $elementkey){

echo Value for key $elementKey = $macroDataArray[$elementKey]br\n;

}

?

(mysql_fetch_array uses MYSQL_BOTH for the second argument by default, which
returns an array using both numeric and associative keys - that's why you
were getting double results. :))


Per the manual:
-
http://www.php.net/manual/en/function.mysql-fetch-array.php

The optional second argument result_type in mysql_fetch_array() is a
constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and
MYSQL_BOTH. This feature was added in PHP 3.0.7. MYSQL_BOTH is the default
for this argument.

By using MYSQL_BOTH, you'll get an array with both associative and number
indices. Using MYSQL_ASSOC, you only get associative indices (as
mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as
mysql_fetch_row() works).
-

HTH,

-Andy



 -Original Message-
 From: Chris Boget [mailto:[EMAIL PROTECTED]]

  $macroDataArray = mysql_fetch_array( $result );

 I'm still curious what is going wrong, but I've found a work arround.
 One of the things I love about PHP is that you learn something new
 just about every day.  Instead of fetch_array(), I can use fetch_assoc()
 and it'll do just what I need. :)


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php