melting_dog Posted October 18, 2011 Share Posted October 18, 2011 Hi guys, Sorry - I know this is a basic question but I've been out of the game for a year now and need to jog my memory. I am trying to count the number of rows per group. e.g: $sql = "SELECT *, COUNT(id) FROM image GROUP BY city"; $result = mysql_query($sql); $row = mysql_fetch_assoc($result); echo $row['COUNT(id)']; So if there are, for example, 5 New York's in the table, 3 London's and 2 Sydney's I want the echo to display 5, 3, 2 Should I use a loop for this? Cheers Link to comment https://forums.phpfreaks.com/topic/249311-noob-question-displaying-count-by-group/ Share on other sites More sharing options...
claro Posted October 18, 2011 Share Posted October 18, 2011 $sql = mysql_query ("SELECT *, COUNT(id) AS ID FROM image GROUP BY city") or die (mysql_error()); while ($row = msyql_fetch_array ($sql)) { echo $row['ID']; } Link to comment https://forums.phpfreaks.com/topic/249311-noob-question-displaying-count-by-group/#findComment-1280159 Share on other sites More sharing options...
melting_dog Posted October 18, 2011 Author Share Posted October 18, 2011 Sweet thanks! Link to comment https://forums.phpfreaks.com/topic/249311-noob-question-displaying-count-by-group/#findComment-1280377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.