dreamwest Posted June 22, 2009 Share Posted June 22, 2009 Im using num_rows to cound how many rows there are in a table, but is count(*) faster Heres what im using: $result3 = mysql_query("SELECT id FROM table WHERE approve='1' ")or die(mysql_error()); $content_count = mysql_num_rows( $result3 ); Also i cant seem to get count to work, it only select about 200 rows, but there are over 30,000 rows $content_count = mysql_query("SELECT COUNT(id) FROM table WHERE approve='1' ")or die(mysql_error()); echo $content_count; Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/ Share on other sites More sharing options...
corbin Posted June 22, 2009 Share Posted June 22, 2009 Yes, COUNT() is most definitely faster. Unless you actually plan on using every set of data you pull, use COUNT(). When ever you use mysql_num_rows, every data set has to be pulled from the SQL server. Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/#findComment-860943 Share on other sites More sharing options...
Ken2k7 Posted June 22, 2009 Share Posted June 22, 2009 <?php $sql = mysql_query('SELECT COUNT(id) cnt FROM table WHERE approve = 1'); $result = mysql_fetch_assoc($sql); echo $result['cnt']; Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/#findComment-860990 Share on other sites More sharing options...
fenway Posted June 22, 2009 Share Posted June 22, 2009 Careful with specifying a field name inside COUNT() -- it's not the same as COUNT(*). Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/#findComment-861009 Share on other sites More sharing options...
Ken2k7 Posted June 22, 2009 Share Posted June 22, 2009 fenway, good point. Since id columns are usually NOT NULL, COUNT(*) would be best. Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/#findComment-861025 Share on other sites More sharing options...
dreamwest Posted June 22, 2009 Author Share Posted June 22, 2009 <?php $sql = mysql_query('SELECT COUNT(id) cnt FROM table WHERE approve = 1'); $result = mysql_fetch_assoc($sql); echo $result['cnt']; Thats did it, it loads much faster now. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/163182-solved-count-or-num_rows/#findComment-861033 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.