showing only unique entries in AutoSuggest

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

showing only unique entries in AutoSuggest

Post 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
Last edited by slaterino on Wed Aug 27, 2008 3:11 pm, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: showing only unique entries in AutoSuggest

Post by onion2k »

Put DISTINCT in the select query.
slaterino
Forum Commoner
Posts: 46
Joined: Fri Jul 11, 2008 10:50 am

Re: showing only unique entries in AutoSuggest

Post 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
Post Reply