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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.