clay1 Posted September 18, 2009 Share Posted September 18, 2009 I have a query which selects all the rows matching a market and then displays how many results there were I need to be able to break that down to display how many total results, and how many were under a certain age Do I need to do a separate select statement? Or can I use something like pg_num_rows to count how many are under age 25? Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/ Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 what does your code currently look like? but you could just iterate throughthe rows. While you iterate, you have several tests, and update several counters based on that test. for example //sql stuff above $arr = array(); $count = 0; $count1 = 0; $count2 = 0; while($row = mysql_fetch_assoc($query)){ $count++; if ($row['age'] < 25){ $count2++; } #etc } Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920831 Share on other sites More sharing options...
clay1 Posted September 18, 2009 Author Share Posted September 18, 2009 That would work Would there be a better way than looping through and counting? Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920835 Share on other sites More sharing options...
mikesta707 Posted September 18, 2009 Share Posted September 18, 2009 not that I could see. you will have to loop through anyways to get the data regardless so its not adding much more stuff for the script to do Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920838 Share on other sites More sharing options...
clay1 Posted September 18, 2009 Author Share Posted September 18, 2009 Cool, thanks Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920844 Share on other sites More sharing options...
Philip Posted September 18, 2009 Share Posted September 18, 2009 What does your current select statement look like? Processing it on the db side is typically more efficient Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920854 Share on other sites More sharing options...
shane18 Posted September 18, 2009 Share Posted September 18, 2009 Try: $rowcount = mysql_num_rows(mysql_query("SELECT NULL FROM TABLENAME WHERE COLUMNNAME < 25")); Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920855 Share on other sites More sharing options...
clay1 Posted September 18, 2009 Author Share Posted September 18, 2009 I'm selecting all from table where market = I'm using just a simple count as suggested to this now It's working Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920867 Share on other sites More sharing options...
The Little Guy Posted September 18, 2009 Share Posted September 18, 2009 mysql_query("SELECT (SELECT COUNT(age) FROM snippets WHERE age = '25') as age25, (SELECT COUNT(age) FROM snippets WHERE age = '30') as age30 WHERE market = 'some value'"); Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920881 Share on other sites More sharing options...
cunoodle2 Posted September 18, 2009 Share Posted September 18, 2009 mysql_num_rows() Quote Link to comment https://forums.phpfreaks.com/topic/174728-solved-count-how-many-rows/#findComment-920896 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.