Reply to Re: newbie: problem with mysql_fetch_array

Your name:

Reply:


Posted by deko on 06/09/06 10:56

> Yes. mysql_fetch_array only fetches one row from the result. In this case it
> fetches an array that looks something like
> array {
> 0 => 1
> cat_ID => 1
> 1 => test
> cat_name => test
> }
>
> It has both numerical and textual indexes. So these are the four elements
> you get in the loop. You can test this with print_r($arrayCat);
>
> Instead of fetching just one row and iterating it's fields, you should fetch
> rows as long as they keep coming.
> instead of the foreach you had there, try something like this:
>
> while($cat = mysql_fetch_array($result)){
> echo "<br>".$cat["cat_ID"]; // = 1
> echo "<br>".$cat["cat_name"]; // = test
> }

This is working:

$sql = "select cat_ID, cat_name from categories where category_count > 0";
mysql_select_db('dbName');
$result = mysql_query($sql);
while ($cat = mysql_fetch_array($result))
{
echo "<br>".$cat["cat_ID"]." = ".$cat["cat_name"];
}

So I was iterating fields in an array rather than rows in a table?

I guess I expected $result to be ALL selected rows, and

$arrayCat = mysql_fetch_array($result);

to be an array of rows.

So I need to loop through each matching row, as I am now doing?

Does this sound correct?

Thanks for the help!

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация