sql question help

Discussion in 'PHP' started by izlik, May 10, 2009.

  1. #1
    in the sql question bellow, how can i make it so the output dont show doubles ? like if the output is "tag,tag1,tag2,tag1" the second tag1 is removed so it's only shown once ?

    <?php
    $sql = "select distinct tags from images";
    $rs = mysql_query($sql);
    while($row=@mysql_fetch_object($rs)){
    print "<a href=http://site.com/tags.php?tag=$row->tags> $row->tags</a> <br> ";
    }
    ?>
    PHP:
     
    izlik, May 10, 2009 IP
  2. Koochook

    Koochook Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hmmmm try $sql = "SELECT *.* FROM images WHERE name = distinct tags";
    if thats how your table's built....
     
    Koochook, May 11, 2009 IP
  3. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #3
    This should be correct sql statement as far as I know :)
     
    harrisunderwork, May 11, 2009 IP
  4. somanweb

    somanweb Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    What is the problem using this code,

    select distinct(tags) from images;

    if you are not getting the result correctly, explain in detail

    Soman
     
    somanweb, May 11, 2009 IP
  5. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #5
    didint work :/

    the problem is that my output is someting like this and i dont want doubles anywhere :/

    Alison,Celebrity
    Pyle,Celebrity
    Kate,Celebrity
     
    izlik, May 11, 2009 IP
  6. somanweb

    somanweb Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    You can not use Distinct keyword for these type of values in the Fiedls.
    You have to use PHP code to achive this

    try the following code, i hope it will help you

    $full_string = '';
    $sql = "select tags from images";
    $rs = mysql_query($sql);
    while($row=@mysql_fetch_object($rs))
    {
    $full_string . = $row->tags;
    $full_string . = ',';
    }
    $full_array = explode(',', $full_string);
    $unique_array = array_unique($full_array);

    Soman
     
    somanweb, May 12, 2009 IP
  7. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #7
    thanks for your response. im getting a "Parse error: syntax error, unexpected '=' " for this line *$full_string . = $row->tags;* :/
     
    izlik, May 12, 2009 IP
  8. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #8
    Give .= and not . =

    This is the error at all lines where u have used this operator.
     
    harrisunderwork, May 13, 2009 IP