MySQL from PHP - Get row of repeating ID only once

Discussion in 'PHP' started by wvccboy, May 21, 2011.

  1. #1
    Hi All,

    Here is what I have to begin with, for example

    ID | DESC
    1 | A
    1 | B
    2 | C

    Here's how I would like the output to be like:

    ID 1:
    - A
    - B

    ID 2:
    - C

    Not sure exactly how I would do this with PHP/MYSQL.

    Ideas would certainly be helpful.

    Thank you! :)
     
    wvccboy, May 21, 2011 IP
  2. niks00789

    niks00789 Well-Known Member

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #2
    You can use 'distinct' keyword to run sql query...

    I would try to give u rough idea see if it helps :

    
    
    mysql_connect("localhost", "user_name", "password") or die(mysql_error()); 
    mysql_select_db("db_name") or die(mysql_error());
    
    $sql = "SELECT DISTINCT id FROM table_name";
    
    $result = mysql_query($sql);
    
    while ($row = mysql_fetch_assoc($result)) 
    {
    
    	$sql = "SELECT desc FROM table_name WHERE id = " . $row['id'];
    	$result2 = mysql_query($sql);
    
    	echo "<strong> ID : " . $row['id'] . "</strong><br/>";
    
    	while ($row2=mysql_fetch_assoc($result2)) 
    	{
    		echo "-" . $row2['desc'] . "<br/>";
    
    	}
    }
    
    
    Code (markup):
    Hopefully this should work :)
     
    niks00789, May 21, 2011 IP
    wvccboy likes this.
  3. echipvina

    echipvina Active Member

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
  4. wvccboy

    wvccboy Notable Member

    Messages:
    2,632
    Likes Received:
    81
    Best Answers:
    1
    Trophy Points:
    250
    #4
    Great. Thanks for the help!
     
    wvccboy, May 23, 2011 IP