Re: [PHP] For Loop going too long

2002-03-30 Thread Lars Torben Wilson

On Sat, 2002-03-30 at 15:40, David Johansen wrote:
 I have a question about something weird that I've noticed Here's some code
 that I have that loads up
 
$sql = SELECT * FROM pickup_times WHERE DAYOFMONTH(time0_name) =
 $dayofmonth;
 
$result = mysql_query($sql, $dbh);
$day = mysql_fetch_array($result);
for ($i=0; $isizeof($day); $i++)
   echo I: $i Result: $day[$i]br;
 
 When I do this it prints out 2 times the number of columns that I actually
 have plus 1. All of the ones past the actual number of columns are just
 empty, but is there something that I'm doing wrong? Thanks,
 Dave

Yup. ;) Give http://www.php.net/mysql_fetch_array a thorough beating.
The function returns the results both in associatively-indexed elements
and in indexed ones, so you get each one twice. Try the following and it
should become clearer:


$result = mysql_query($sql, $dbh);
// Try both of the following lines and notice the difference.
//$day = mysql_fetch_array($result);
$day = mysql_fetch_array($result, MYSQL_ASSOC);
foreach ($day as $colname = $value) {
echo Column name: $colname; Value: $value\n;
}


Cheers!

-- 
 Torben Wilson [EMAIL PROTECTED]
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


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




RE: [PHP] For Loop going too long

2002-03-30 Thread Demitrious S. Kelly

Try a foreach... it works well...

-Original Message-
From: Lars Torben Wilson [mailto:[EMAIL PROTECTED]] On Behalf Of Lars
Torben Wilson
Sent: Saturday, March 30, 2002 4:38 PM
To: David Johansen
Cc: [EMAIL PROTECTED]
Subject: Re: [PHP] For Loop going too long

On Sat, 2002-03-30 at 15:40, David Johansen wrote:
 I have a question about something weird that I've noticed Here's some
code
 that I have that loads up
 
$sql = SELECT * FROM pickup_times WHERE DAYOFMONTH(time0_name) =
 $dayofmonth;
 
$result = mysql_query($sql, $dbh);
$day = mysql_fetch_array($result);
for ($i=0; $isizeof($day); $i++)
   echo I: $i Result: $day[$i]br;
 
 When I do this it prints out 2 times the number of columns that I
actually
 have plus 1. All of the ones past the actual number of columns are
just
 empty, but is there something that I'm doing wrong? Thanks,
 Dave

Yup. ;) Give http://www.php.net/mysql_fetch_array a thorough beating.
The function returns the results both in associatively-indexed elements
and in indexed ones, so you get each one twice. Try the following and it
should become clearer:


$result = mysql_query($sql, $dbh);
// Try both of the following lines and notice the difference.
//$day = mysql_fetch_array($result);
$day = mysql_fetch_array($result, MYSQL_ASSOC);
foreach ($day as $colname = $value) {
echo Column name: $colname; Value: $value\n;
}


Cheers!

-- 
 Torben Wilson [EMAIL PROTECTED]
 http://www.thebuttlesschaps.com
 http://www.hybrid17.com
 http://www.inflatableeye.com
 +1.604.709.0506


-- 
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