jeboy Posted May 27, 2008 Share Posted May 27, 2008 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); Quote Link to comment Share on other sites More sharing options...
Xurion Posted May 27, 2008 Share Posted May 27, 2008 For ONLY checking the number of rows, use the mysql count method. Quote Link to comment Share on other sites More sharing options...
jeboy Posted May 28, 2008 Author Share Posted May 28, 2008 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? Quote Link to comment Share on other sites More sharing options...
Xurion Posted May 28, 2008 Share Posted May 28, 2008 mysql_num_rows passes the information to PHP, whereas COUNT does not, unless you specify it to do so. Quote Link to comment Share on other sites More sharing options...
fenway Posted May 29, 2008 Share Posted May 29, 2008 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? Quote Link to comment Share on other sites More sharing options...
jeboy Posted May 30, 2008 Author Share Posted May 30, 2008 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? Quote Link to comment Share on other sites More sharing options...
Xurion Posted May 30, 2008 Share Posted May 30, 2008 For ONLY checking the number of rows, use the mysql count method. Quote Link to comment Share on other sites More sharing options...
fenway Posted May 30, 2008 Share Posted May 30, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.