Search code examples
phpmysqldistinctautosuggest

distinct query with autosuggest


I'm trying to distinct a column for my autosuggest function. This is the query I have now:

$result=mysql_query("SELECT * FROM users WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%' ORDER BY firstname LIMIT 0, 10",$con) or die(mysql_error());

somehow just adding 'DISTINCT firstname' after select doesn't work. (Javascript gives an error.) the * in the query is the trouble maker I guess, don't know exactly why..

Please assist with writing the right query! :)

Thanks in advance


Solution

  • You could use "group by firstname" in stead:

    $result=mysql_query("
        SELECT * FROM users
        WHERE firstname LIKE '%".mysql_real_escape_string($_GET['chars'])."%'
        GROUP BY firstname ASC
        ORDER BY firstname
        LIMIT 0, 10",$con)
    or die(mysql_error());