removing duplicate data from SQL table

Discussion in 'PHP' started by bumbar, Apr 4, 2007.

  1. #1
    Hallo!
    I have table with this structure:

    
    CREATE TABLE `product` (
      `id` int(11) NOT NULL auto_increment,
      `name` text NOT NULL,
      PRIMARY KEY  (`id`)
    ) ;
    
    INSERT INTO `product` (`id`, `name`) VALUES 
    (1, 'car'),
    (2, 'car'),
    (3, 'motor'),
    (4, 'motor');
    
    
    PHP:

    
    mysql_connect ("localhost", "user", "pass");
    
    mysql_select_db ("catalog");
    
    $select = "SELECT id, name FROM `product`";
    
    $query = mysql_query ($select);
    
    echo "<table border=1>"; 
    
    while ($row = mysql_fetch_array ($query)) {
    		
    	echo "<tr><td>" . "<b>" . $row['id'].  "</b>" . "</td><td>" . $row['name'] . "</td></tr>";
    	
    }
    echo "</table>";
    
    PHP:
    This output:
    1--car
    2--car
    3--motor
    3--motor

    How in while condition I can restrict duplicate data?
    I want retrieve:

    1--car
    2--motor

    Any suggestion?
    Thanks!
     
    bumbar, Apr 4, 2007 IP
  2. boyponga

    boyponga Banned

    Messages:
    1,013
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this code in your SQL, in your select command:

     
    boyponga, Apr 4, 2007 IP
  3. bumbar

    bumbar Active Member

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Thank you!
    Because I binding this output with another table, I want to using some PHP formatting in WHILE loop. Just eliminate duplicate row with PHP code ...:confused:
     
    bumbar, Apr 4, 2007 IP
  4. kashem

    kashem Banned

    Messages:
    1,250
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    0
    #4
    while ($row = mysql_fetch_array ($query)) {
    if ($sname !=$row['name'])
    {
    echo "<tr><td>" . "<b>" . $row['id']. "</b>" . "</td><td>" . $row['name'] . "</td></tr>";
    }
    $sname !=$row['name']);

    }

    try with it , if it helps you
     
    kashem, Apr 4, 2007 IP