Cless Posted July 12, 2009 Share Posted July 12, 2009 For a query such as: SELECT count(*) FROM table LIMIT 5 What would be the best way to count the rows found? I'm aware you can use: mysql_num_rows (though I don't think you can use count() with this), mysql_result, and mysql_fetch_row, depending on the circumstance. I've heard mysql_result($rows, 0) is the most efficient, however, if there are no rows found, it will give an error. So what do you recommend? Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/ Share on other sites More sharing options...
Q695 Posted July 12, 2009 Share Posted July 12, 2009 The best way would be found at:http://us2.php.net/manual/en/function.mysql-num-rows.php Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-873882 Share on other sites More sharing options...
Cless Posted July 12, 2009 Author Share Posted July 12, 2009 So is that just another way of saying that mysql_num_rows() is best? =P Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-873885 Share on other sites More sharing options...
Q695 Posted July 12, 2009 Share Posted July 12, 2009 Yes, it counts the number of rows, and you can do a while loop to output what you want. One of the limit options give you a set, and the other is the multiplyer for the first number in the set. Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-874089 Share on other sites More sharing options...
Daniel0 Posted July 12, 2009 Share Posted July 12, 2009 No, it is not the same. If you're looking to count the number of rows, without using those rows you should use MySQL's COUNT() function. There is a difference in the amount of work MySQL has to do. Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-874114 Share on other sites More sharing options...
Q695 Posted July 13, 2009 Share Posted July 13, 2009 I never knew there was a count command for mysql other than limit. Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-874308 Share on other sites More sharing options...
Daniel0 Posted July 13, 2009 Share Posted July 13, 2009 Then read the topic you replied to Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-874384 Share on other sites More sharing options...
Andy-H Posted July 13, 2009 Share Posted July 13, 2009 LIMIT simply LIMITS the amount of rows returned by the query before telling mysql to stop searching. COUNT() counts and returns the number of rows returned and can be accessed when fetching the data. Although I'm not sure how it's used when fetched unless used as- "SELECT COUNT(*), fieldName As num FROM table" //Then when its pulled into an associative array, for example you can access it using $count = $row['num']; Quote Link to comment https://forums.phpfreaks.com/topic/165661-row-counting/#findComment-874386 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.