|
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]
|