pressriot Posted December 11, 2008 Share Posted December 11, 2008 Hello, first time poster here, looking for some Mysql query advice. I want to 'categorize' my results by a given column. My table is something like Menu title description catid The query I tried (several different ways) was essentially SELECT * FROM menu GROUP BY catid The problem is, with this code it only outputs 1 result for each value in the CATID column. What i want it to do is show all of them. Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/ Share on other sites More sharing options...
fenway Posted December 11, 2008 Share Posted December 11, 2008 I don't undersatnd. Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/#findComment-712615 Share on other sites More sharing options...
pressriot Posted December 11, 2008 Author Share Posted December 11, 2008 Sorry, I don't know the exact terminology for everything which is probably what is causing my problems. I need a query that will grab all the rows in the database and group the rows with identical values in the column named 'catid'. Here is the code i am using. $query = "SELECT * FROM menu GROUP BY catid"; $result = mysql_query($query) or die('Error : ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo "<h3>{$row['category']} </h3>"."<p>{$row['title']} -" . "{$row['description']} <br />" . "Price : $"."{$row['price']} <br /><br /></p>"; } This does basically what I want but it only shows one result per catid. I want to show everything in the table but still maintain this overal idea. If anyone can help or point me in the right direction I would greatly appreciate it. Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/#findComment-712637 Share on other sites More sharing options...
fenway Posted December 11, 2008 Share Posted December 11, 2008 I'm confused by your call to fetch_array() outsdie the while loop. But regardless, it sounds like you want GROUP_CONCAT(). Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/#findComment-712714 Share on other sites More sharing options...
pressriot Posted December 11, 2008 Author Share Posted December 11, 2008 I had tried group_concat also and it still returns only one row. Maybe I am going about this the wrong way. Originally I was going to use a different query for each category. SELECT * FROM menu WHERE catid=1 produces the results I want, but doing this query over and over for each row in the catid column seems wrong to me and there must be a better way? Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/#findComment-712797 Share on other sites More sharing options...
fenway Posted December 11, 2008 Share Posted December 11, 2008 I had tried group_concat also and it still returns only one row. That's probably because of that strange code you're using. Check mysql_num_rows() after the query() call. Link to comment https://forums.phpfreaks.com/topic/136517-need-help-with-mysql-query-grouping/#findComment-712837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.