vet911 Posted March 27, 2012 Share Posted March 27, 2012 I'm having trouble with group_concat and could use some help. When i run the code as shown below, I get a return of this: ( 21Three D MachineB-10, 3-1, 1-8, 2-1, 1-3, 4-6, 1-1, B-1, 3-8, 4-2, 3-6, 1-13, 1-7, B-9, B-5, 1-5, 3-7, 1-2, B-2, 1-6, 4-5, 3-10, 3-2, 2-7, 3-4, B-17, 4-7, B-4, 2-3, 2-4, 2-2, 2-5, 2-4, 2-6, 2-8, B-11, B-14, B-16, 1-9, 1-11, B-12, B-13, 3-11, 4-1, 4-3, 4-8, 4-4) not including the brackets. When I change the code to include: WHERE id_unit = 7, i get this result (7Joyce C & H Inc.B-5, B-4, 2-4) which is only one row of the table. I'm looking for help in fixing the code to list all the id_unit that should be listed, approx 20 items. I believe what is happening is the group_concat is using all the numbers in the unit column instead of just the ones related to the id_unit number. Any help would be greatly appreciated. Thanks in advance. <?php // Make a MySQL Connection include 'config_******.php'; $con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db("*****") or die(mysql_error()); $query = "SELECT *, GROUP_CONCAT(unit SEPARATOR ', ') as value_list FROM units_1"; /*WHERE id_unit = 7";*/ $result = mysql_query($query); if($result && mysql_num_rows($result) > 0) { $i = 0; while($row=mysql_fetch_array($result)) { extract($row); if($id_unit != "" && $id_unit != null) echo "$row[id_unit]"; echo "$row[company]"; echo "$row[value_list]"; } // end while } // end if results mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/259783-im-having-trouble-with-group_concat-and-could-use-some-help/ Share on other sites More sharing options...
vet911 Posted March 27, 2012 Author Share Posted March 27, 2012 I figured it out, it just took a while. This is what I did added GROUP BY company ASC";, now works fine for this part. On to the next part. <?php include 'config_vetmanpc.php'; $con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql'); mysql_select_db("vetmanpc") or die(mysql_error()); $query = "SELECT *, GROUP_CONCAT(unit SEPARATOR ', ') as value_list FROM units_1 GROUP BY company ASC"; $result = mysql_query($query); if($result && mysql_num_rows($result) > 0) { $i = 0; while($row=mysql_fetch_array($result)) { extract($row); if($i == 0) if($id_unit != "" && $id_unit != null) echo "$row[id_unit]"; echo "$row[company]"; echo "$row[value_list]"; echo "<br />"; } } mysql_close($con); ?> Link to comment https://forums.phpfreaks.com/topic/259783-im-having-trouble-with-group_concat-and-could-use-some-help/#findComment-1331734 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.