Hugo Sousa
Hugo Sousa

Reputation: 916

jQuery autocomplete returns some null values

I'm using jQuery autocomplete with PHP source file that connects to MySQL and get the info to show as autocomplete on input field. Here's my code:

Index/Input

<script>

 $(function() {
     $("#search").autocomplete({
    source: 'http://localhost/testes/autocomplete.php',
    minLength: 3
 });
});

</script>

<input type="text" id="search"/>

autocomplete PHP

$req = "SELECT DISTINCT name FROM faculty WHERE name LIKE '%".$_REQUEST['term']."%'"; 
$query = mysql_query($req);

while($row = mysql_fetch_array($query)){
    $results[] = array('label' => $row['name']);
}

echo json_encode($results);

The problem is, it returns good values and other null values. But, in the last case, the values should not be null because they are in the database.

For example, in the database I have the entries: ISCTE - Instituto Universitário INDEG-ISCTE Business School

Searching by 'iscte' the autocomplete gives second one but the first one appear as null.

Thank you for you time, Regards, Hugo

Upvotes: 0

Views: 1203

Answers (1)

Manuhel
Manuhel

Reputation: 11

That is because the encoding. Use this:

...
while($row = mysql_fetch_array($query)){
    $results[] = array('label' => utf8_encode($row['name']));
}
...

Your database should set to UTF8, but by the way, this fix it.

Upvotes: 1

Related Questions