Page 1 of 1

showing only unique entries in AutoSuggest

Posted: Wed Aug 27, 2008 3:59 am
by slaterino
Hi,
I am using an AutoSuggest box that runs from a MySQL database. However, I have one problem which is that my database has multiple entries of certain words so for example, if someone typed in 'd' they would get a list of the word 'doctors' ten times. How can I amend my code so that it only lists unique entries? Below is the code for the backend:

Code: Select all

<?php
$db = new mysqli('129.0.0.1', 'asad' ,'asad', 'asad');
    if(!$db) {
        echo 'ERROR: Could not connect to the database.';
    } else {
        if(isset($_POST['queryString'])) {
            $queryString = $db->real_escape_string($_POST['queryString']);
 
            if(strlen($queryString) >0) {
                
                $query = $db->query("SELECT OrgType FROM Contacts WHERE OrgType LIKE '$queryString%' LIMIT 10");
                if($query) {
 
                    while ($result = $query ->fetch_object()) {
                        
                        echo '<li onClick="fill(\''.$result->OrgType.'\');">'.$result->OrgType.'</li>';
                    }
                } else {
                    echo 'ERROR: There was a problem with the query.';
                }
            } else {
 
            } 
        } else {
            echo 'There should be no direct access to this script!';
        }
    }
?>
Thanks
Russ

Re: showing only unique entries in AutoSuggest

Posted: Wed Aug 27, 2008 4:09 am
by onion2k
Put DISTINCT in the select query.

Re: showing only unique entries in AutoSuggest

Posted: Wed Aug 27, 2008 3:11 pm
by slaterino
Hey,
Thanks for that. I'm glad it was something so simple! How would I go about adding a POST field to draw up all the entries from the database? Would that be something just as simple?

Thanks!!
Russ