Unknown98 Posted July 13, 2011 Share Posted July 13, 2011 I have a table, with about a hundred cars in it. There's three columns, id, car name, and one for manufacturer. What I'd like to do, is display a table listing all manufacturers and how many cars each has assigned to it. For example, say I have 5 Dodge cars in the table and 7 Fords. I'd like it to look like this: Dodge | 5 Ford | 7 etc... for all manufacturers. I think I would have to use some form of COUNT() in my query correct? And would I need a for or while loop? Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/ Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 Do it all in your MySQL statement. SELECT Make, COUNT(*) FROM `cars` GROUP BY Make Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242070 Share on other sites More sharing options...
Unknown98 Posted July 13, 2011 Author Share Posted July 13, 2011 So, something like this? $query = sprintf("SELECT car_manufacturer, COUNT(*) FROM `car_pool` GROUP BY car_manufacturer"); How would I go about displaying it on the page? Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242074 Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 $sql="SELECT car_manufacturer, COUNT(*) FROM `car_pool` GROUP BY car_manufacturer"; if ($query=@mysql_query($sql)) { if (@mysql_num_rows($query)) { while ($req=@mysql_fetch_array($query)) { echo $req[0] ."|".$req[1]; } } } else { die(mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242076 Share on other sites More sharing options...
jcbones Posted July 13, 2011 Share Posted July 13, 2011 You don't need sprintf() on that string. $sql = 'SELECT car_manufacturer, COUNT(*) as car_count FROM car_pool GROUP BY car_manufacturer'; $result = mysql_query($sql) or trigger_error($sql . ' has an error<br />' . mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { echo $row['car_manufacturer'] . ' | ' . $row['car_count'] . '<br />'; } } else { echo 'There is NO cars selected!'; } Teynon has a typo, this should work. Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242077 Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 Where's the typo? I'm not seeing it. Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242080 Share on other sites More sharing options...
jcbones Posted July 13, 2011 Share Posted July 13, 2011 Calling the same function twice, which doesn't get the data. if (@mysql_num_rows($query)) { while ($req=@mysql_num_rows($query)) { Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242082 Share on other sites More sharing options...
teynon Posted July 13, 2011 Share Posted July 13, 2011 Ah, didn't notice that. Heh. Fixed. Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242083 Share on other sites More sharing options...
Unknown98 Posted July 13, 2011 Author Share Posted July 13, 2011 Ah! It's working perfectly Thanks teynon & jcbones. Quote Link to comment https://forums.phpfreaks.com/topic/241860-display-list-of-cars-from-mysql/#findComment-1242338 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.