Home Calling mysql_fetch_assoc only returns first result, even when called multiple times (CLOSED: TYPO)
Reply: 0

Calling mysql_fetch_assoc only returns first result, even when called multiple times (CLOSED: TYPO)

user1521
1#
user1521 Published in April 20, 2018, 10:48 am

EDIT: Phil's comment on making the MySQL_query into a variable instead of running it through the while loop was the correct answer!

I'm am trying to print out all the items in an item table using the MySQL_fetch_assoc operator. It only returns the first result even when called in a while loop, but when called through the command line running, it returns all of the correct results. I have tried to run the while command without the$numRows > 0 conditional but then it just returns an infinite array with only the first result.

...
$query = "SELECT * FROM item JOIN seller ON item.seller_id = seller.seller_id;";
$numRows = MySQL_num_rows(MySQL_query($query));
while ($numRows > 0 && $row = MySQL_fetch_assoc(MySQL_query($query))) {
echo "<tr><td>" . $row["item_name"] . "</td>\n" .
" <td>" . $row["category"] . "</td>\n" .
" <td>" . $row["item_price"] . "</td>\n" .
" <td>" . $row["user_email"] . </td>\n" .
" </tr>\n";
$numRows--;
...

It should be return a table of all the items

Name Category Price Seller Email
cup  utensils 200  bob@email.com
shaker utensils 300 sam@email.com
spoon  utensils 400 james@email.com

but only returns a table with the first result

Name Category Price Seller Email
cup  utensils 200   bob@email.com
cup  utensils 200   bob@email.com
cup  utensils 200   bob@email.com
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31056 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO