How to avoid duplicates with MySQl in automatic suggestion

advertisements

How can I avoid duplicate return data with MYSQL with autosuggest with this code? Thank you so much for helping

   <?php
include('conn.php');
$str = strtolower($_GET['content']);
if(strlen($str))
{
    $sel = mysql_query("select * from Streams where title like '".trim($str)."%'");
    if(mysql_num_rows($sel))
    {
        echo "<table border =\"0\" width=\"100%\">\n";
        if(mysql_num_rows($sel))
        {
            echo "<script language=\"javascript\">box('1');</script>";
            while($row = mysql_fetch_array($sel))
            {
                $country = str_ireplace($str,"<b>".$str."</b>",($row['title']));
                echo "<tr id=\"word".$row['title']."\" onmouseover=\"highlight(1,'".$row['title']."');\" onmouseout=\"highlight(0,'".$row['title']."');\" onClick=\"display('".$row['title']."');\" >\n<td>".$country."</td>\n</tr>\n";
            }
        }
        echo "</table>";
    }
}
else
{
    echo "<script language=\"javascript\">box('0');</script>";
}
?>


What I can see in your code is that there is only a single field - title - from the SQL resultset that you are using in the PHP code. So why not instead write your query as:

"SELECT DISTINCT `title`
FROM Streams
WHERE title like '".trim($str)."%'"

Or perhaps if you can't change the query, you may store the title in a PHP array and then run array_unique on it to avoid duplicates before writing them to HTML.