Smee Posted June 7, 2010 Share Posted June 7, 2010 Hey, I have been playing around with a count function and have it nearly working how i want it to but cant figure out what is going wrong. The code is returning these results: There are 11 11 supporters. There are 9 9 supporters. There are 2 2 supporters. There are 2 2 supporters. There are 1 1 supporters. There are 1 1 supporters. The second number(s) should be the names of teams but i can't seem to get the names to show. The reason for this is because of the duplication of . $row['team_supported'] ." but if i have it like . $row['COUNT(team_supported)'] ." i get an error of undefined variable. Anyway here is what i have done so far. <?php $query = "SELECT team_supported, COUNT(team_supported) AS team_supported FROM users GROUP BY team_supported ORDER BY team_supported DESC LIMIT 6"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo "There are ". $row['team_supported'] ." ". $row['team_supported'] ." supporters."; echo "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/204110-count-function/ Share on other sites More sharing options...
ignace Posted June 7, 2010 Share Posted June 7, 2010 SELECT team_supported, COUNT(team_supported) AS team_supported_count Link to comment https://forums.phpfreaks.com/topic/204110-count-function/#findComment-1069068 Share on other sites More sharing options...
Smee Posted June 7, 2010 Author Share Posted June 7, 2010 Thanks again Ignace works a treat! Link to comment https://forums.phpfreaks.com/topic/204110-count-function/#findComment-1069083 Share on other sites More sharing options...
GoneNowBye Posted June 7, 2010 Share Posted June 7, 2010 Just for the record, count(*) with group statements, counts the number of rows in that group. Link to comment https://forums.phpfreaks.com/topic/204110-count-function/#findComment-1069119 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.