marf Posted June 24, 2007 Share Posted June 24, 2007 I know how to do this, but my question is I have come across 3 ways. First is SELECT count(*) Second is SQL_CALC_FOUND_ROWS Third is $result = mysql_query(...) mysql_num_rows($result) So what is the norm? What is best practice these days? I know that count(*) is the older method and I think its depreciated, SQL_CALC_FOUND_ROWS is for version 4 and up I believe. Quote Link to comment https://forums.phpfreaks.com/topic/56962-methods-to-count-number-of-rows-in-a-select/ Share on other sites More sharing options...
Zane Posted June 24, 2007 Share Posted June 24, 2007 there isn't really a norm to it. You just find the one that works best for you and stick with it. I personally like to count them at query time...using COUNT(*) as whatever...then I can just access that variable later. If you know you're gonna need that count a lot in your code I'd recommend using COUNT(*) to save code if you just need only one time...then use mysql_num_rows() Quote Link to comment https://forums.phpfreaks.com/topic/56962-methods-to-count-number-of-rows-in-a-select/#findComment-281362 Share on other sites More sharing options...
marf Posted June 24, 2007 Author Share Posted June 24, 2007 is any one method "faster" or more Efficient in the SQL? like does a query with SQL_CALC_FOUND_ROWS execute faster than count(*) ? Quote Link to comment https://forums.phpfreaks.com/topic/56962-methods-to-count-number-of-rows-in-a-select/#findComment-281450 Share on other sites More sharing options...
bubblegum.anarchy Posted June 25, 2007 Share Posted June 25, 2007 SELECT count(*) is fastest Quote Link to comment https://forums.phpfreaks.com/topic/56962-methods-to-count-number-of-rows-in-a-select/#findComment-281732 Share on other sites More sharing options...
fenway Posted June 27, 2007 Share Posted June 27, 2007 SELECT count(*) is fastest Not always... it really depends what you're trying to accomplish, and have the LIMIT & OFFSET are like. Sure, it's easier just to write a single query and "cheat" with calc_rows, sometimes it's better and sometimes it's worse... depends on all sorts of things, group bys, having clauses, etc. No hard and fast rule. Quote Link to comment https://forums.phpfreaks.com/topic/56962-methods-to-count-number-of-rows-in-a-select/#findComment-284298 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.