Re: For Loop going too long
| From: | Lars Torben Wilson | Date: | Sun, 31 Mar 2002 00:37:41 +0000 |
| Subject: | Re: For Loop going too long | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-90833@lists.php.net to get a copy of this message | ||
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; $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 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 <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506