Jump to content

What is the better way of knowing the number of rows in a result set?


jeboy

Recommended Posts

What is the better way to know the number of rows in a result set when it comes to large results?

 

e.g.

$sql = "SELECT PK_ProductId, ProductName, FK_CategoryId FROM tbl_products WHERE FK_CategoryId = 1";

$result = mysql_query($sql);

 

option 1:

$totalRows = mysql_num_rows($result);

 

option 2:

$sql = "SELECT COUNT(*) FROM tbl_products WHERE FK_CategoryId = 1";

$result = mysql_query($sql);

For ONLY checking the number of rows, use the mysql count method.

 

Why? What is the performance difference between mysql_num_rows and using COUNT in the query?

If you query matches 10,000 rows, why send all 10,000 rows from the server to the client, create a PHP object, and then have to count them, when you can simply return a single number?

If you query matches 10,000 rows, why send all 10,000 rows from the server to the client, create a PHP object, and then have to count them, when you can simply return a single number?

 

So what is your point? It's better to use the COUNT function in the query rather than mysql_num_rows() function of PHP?

If you query matches 10,000 rows, why send all 10,000 rows from the server to the client, create a PHP object, and then have to count them, when you can simply return a single number?

 

So what is your point? It's better to use the COUNT function in the query rather than mysql_num_rows() function of PHP?

YES -- that's exactly my point... if you only want the actual value of the count.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.