Jump to content
  • Advertisement
Sign in to follow this  
uncle_rico

[web] Resolved: Counting how many rows retrieved from mySQL SELECT query in PHP?

This topic is 4438 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I was thinking I could use count(), but that doesn't seem to work. I think it's because mysql_query() returns a resource instead of an array. Are there any similar functions for resources? Edit: I figured out. I apologize for being too quick to post! For those who don't know, the type of resource that the mysql_query() returns is a "MySQL Result", and if your query was a SELECT, you can feed the result to mysql_num_rows() and it will return the number of rows in the result.

Share this post


Link to post
Share on other sites
Advertisement
Even better: Do it in MySQL.


$result = mysql_query("SELECT COUNT(*) FROM my_table");
$row = mysql_fetch_row($result);
$count = $row[0];
mysql_free_result($result);

Share this post


Link to post
Share on other sites
Quote:
Original post by Sander
Even better: Do it in MySQL.


$result = mysql_query("SELECT COUNT(*) FROM my_table");
$row = mysql_fetch_row($result);
$count = $row[0];
mysql_free_result($result);


that will return the number of rows in a table, and not the number of rows returned by a query, as the OP asked for.

Like you discovered, mysql_num_rows() is what you want.

Share this post


Link to post
Share on other sites
Quote:
Original post by gavco98
that will return the number of rows in a table, and not the number of rows returned by a query, as the OP asked for.


Ah, I misread that. mysql_num_rows() is indeed what the OP needs.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!