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
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
> 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; $i<sizeof($day); $i++)
>       echo "I: $i Result: $day[$i]<br>";
> When I do this it prints out 2 times the number of columns that I
> have plus 1. All of the ones past the actual number of columns are
> empty, but is there something that I'm doing wrong? Thanks,
> Dave

Yup. ;) Give 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";


 Torben Wilson <[EMAIL PROTECTED]>

PHP General Mailing List (
To unsubscribe, visit:

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to