phpmikey Posted March 31, 2010 Share Posted March 31, 2010 hi I would like to be able produce statistics on my web page by counting the numbers of rows in my sql database. for example. within my database i have 10 defects in my table called "defects". 9 defects are major, 1 defect is a minor. is there a way to extract this data in numeric form rather than printing the values as they come? i would like statistics in my web page to tell me how many defects are major and how many are minor. major defects: 9 minor defects: 1 something like: echo count FROM defects WHERE name=major eventually i would like to be able to produce graphs but i think that is a bit beyond me but simple tables showing the statistics would be very useful for a start. thank you for any help you can offer Quote Link to comment https://forums.phpfreaks.com/topic/197061-statistics-counting-mysql-rows-and-display-a-number/ Share on other sites More sharing options...
ale8oneboy Posted March 31, 2010 Share Posted March 31, 2010 hi I would like to be able produce statistics on my web page by counting the numbers of rows in my sql database. for example. within my database i have 10 defects in my table called "defects". 9 defects are major, 1 defect is a minor. is there a way to extract this data in numeric form rather than printing the values as they come? i would like statistics in my web page to tell me how many defects are major and how many are minor. major defects: 9 minor defects: 1 something like: echo count FROM defects WHERE name=major eventually i would like to be able to produce graphs but i think that is a bit beyond me but simple tables showing the statistics would be very useful for a start. thank you for any help you can offer I'm not sure exactly how your table is designed. But from what I gather, you could do single query to get the data you need. Try something like: $sql = "SELECT name, count(*) FROM defects GROUP BY name"; This query would result with each name, and the count of that name. If that doesn't help. Try posting a copy of your table and I may be able to help you better. Quote Link to comment https://forums.phpfreaks.com/topic/197061-statistics-counting-mysql-rows-and-display-a-number/#findComment-1034466 Share on other sites More sharing options...
phpmikey Posted March 31, 2010 Author Share Posted March 31, 2010 thanks for your reply. the database table structure is id severity description 1 major description example 2 major description example 3 minor description example so the php code i would like to have would count the number of rows with the "severity" name 'major' and post 2, and count the number of rows with 'minor' and display a 1. then i could echo these numbers into a statistics table like such: major: 2 minor: 1 i hope this explains. would the example code you posted achieve this? Quote Link to comment https://forums.phpfreaks.com/topic/197061-statistics-counting-mysql-rows-and-display-a-number/#findComment-1034472 Share on other sites More sharing options...
phpmikey Posted March 31, 2010 Author Share Posted March 31, 2010 can anyone help with this? thanks Quote Link to comment https://forums.phpfreaks.com/topic/197061-statistics-counting-mysql-rows-and-display-a-number/#findComment-1034604 Share on other sites More sharing options...
ale8oneboy Posted March 31, 2010 Share Posted March 31, 2010 thanks for your reply. the database table structure is id severity description 1 major description example 2 major description example 3 minor description example so the php code i would like to have would count the number of rows with the "severity" name 'major' and post 2, and count the number of rows with 'minor' and display a 1. then i could echo these numbers into a statistics table like such: major: 2 minor: 1 i hope this explains. would the example code you posted achieve this? Let me give you a little sample code based off what you've given. This isn't the full code. You'll need statements to connect to the database. But this will display the information in the format you're looking for. $sql = "SELECT severity, count(*) FROM defects GROUP BY severity"; $result = mysql_query($sql); while($row = mysql_fetch_array($result, MYSQL_NUM)) { echo "$row[0]: $row[1]"; } Quote Link to comment https://forums.phpfreaks.com/topic/197061-statistics-counting-mysql-rows-and-display-a-number/#findComment-1034640 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.