tags:

views:

46

answers:

3

I have the following query:

    $select = mysql_query("SELECT * FROM posts WHERE id = $postIds");

    while ($return = mysql_fetch_assoc($select)) {

        $postUrl      = $return['url'];
        $postTitle    = $return['title'];

        echo "<h1><a href='$postUrl'>".$postTitle."</a></h1>";

    }

Now the problem is, the variable $postIds often times contain the same id multiple times. So the title of the post echos itself multiple times. Is there a way to have it echo only once?

A: 

Use DISTINCT in your query: SELECT DISTINCT url, title FROM posts WHERE id =

Frank Heikens
A: 

http://php.net/manual/en/function.array-unique.php

You can give that a shot. You might want to make a check to ensure $postIDs will be an array, or you'll probably get a warning error.

Jeff Rupert
A: 

You want all the posts to be output, but only output the title(s) once? Use SELECT DISTINCT title, url FROM posts WHERE id = $postIds; for your query

Or do you only want the first matching record output? Two choices here:

  1. Add a LIMIT clause to the query: "SELECT * FROM posts WHERE id = $postIds LIMIT 1";
  2. Eliminate the while() loop in the script and just call the fetchrow once.
Marc B