Re: array count() oddity
| From: | Kevin Stone | Date: | Fri, 30 May 2003 21:24:14 +0000 |
| Subject: | Re: array count() oddity | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-149602@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: "Guru Geek" <gurugeek2002@g-i-w-s.com>
To: <php-general@lists.php.net>
Sent: Friday, May 30, 2003 3:15 PM
Subject: [PHP] array count() oddity
> Hello,
>
> I have a mysql statement listed below. The table contains about 100
> rows of information. Only one row matches the $criteria. Can anyone
> tell me why the count($myrow) is 8? Shouldn't it be 4 (id, vanNumber,
> origin, destination)?
>
> $criteria = "02-25-2003";
> $result = mysql_query("SELECT id, vanNumber, origin, destination FROM
> thisHereTable WHERE date='$criteria'");
> $myrow = mysql_fetch_array($result);
>
> And when I run the rest of the script, it prints out the results 8 times
> instead of once....
> for ($count=0; $count<count($myrow); $count++)
> {
> print "<p>".$myrow["vanNumber"];
> print "<br>".$myrow["origin"]." to
> ".$myrow["destination"];
> }
>
> THANKS,
> Roger
Hi,
mysql_fetch_array(); produces two arrays, one indexed and one associative,
thus count() will return twice the expected number. Use mysql_fetch_row()
to retrieve a single indexed array or mysql_fetch_assoc() to retrieve a
single associative array.
Refer to the documentation:
http://us2.php.net/manual/en/function.mysql-fetch-array.php
- Kevin