Reply to Re: newbie: problem with mysql_fetch_array

Your name:

Reply:


Posted by David Haynes on 06/09/06 09:58

deko wrote:
> I'm trying to select a few rows:
>
> $db = mysql_connect('localhost','userID','passwd');
> if ($db)
> {
> $sql = "select cat_ID, cat_name from categories where category_count > 0";
> mysql_select_db('dbName');
> $result = mysql_query($sql);
> $arrayCat = mysql_fetch_array($result);
> }
> mysql_close;
>
> echo count($arrayCat); //count = 4
>
> foreach ($arrayCat as $cat)
> {
> // this will iterate 4 times
> echo "<br>".$arrayCat["cat_ID"]; // = 1
> echo "<br>".$arrayCat["cat_name"]; // = test
> }
>
> what I don't understand is I get an array count of 4, but if I look in
> the table, I see only 3 rows that meet the select criteria.
> Furthermore, why does each iteration echo the same thing:
>
> 1
> test
> 1
> test
> 1
> test
> 1
> test
>
> Am I using mysql_fetch_array wrong?
>
> Thanks in advance.
>
>

In your 'foreach' you are assigning the individual elements of
'arrayCat' to a variable called 'cat', but in your echo, you are using
'arrayCat' again.

Change your foreach to this:
foreach ($arrayCat as $cat) {
echo "<br>".$cat["cat_ID"]; // = 1
echo "<br>".$cat["cat_name"]; // = test
}

-david-

[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

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