Jump to content

I'm having trouble with group_concat and could use some help.


vet911

Recommended Posts

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);
?>

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.