Sign in to follow this  
uncle_rico

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this