Frederik
Frederik

Reputation: 652

Jquery UI autocomplete MySQL/PHP

I got this PHP code:

// connect to mysql
require_once('includes/connect.php');
// include config
include('includes/config.php');

$nameser = $_GET['term'];

$search = Array();
$names = '';
$result = mysql_query("SELECT name FROM customers WHERE name LIKE '%".$nameser."%'");
while ($row = mysql_fetch_assoc($result))
    $names = json_encode($row['name']);

echo $names;

But the output is not formatted right, so the autocomplete script cannot figure out what it should do with it.

Also, this example only outputs 1 entry, but there should be far more than that.

Any ideas?

Upvotes: 2

Views: 1566

Answers (1)

flowfree
flowfree

Reputation: 16462

Here is the correct code:

$names = array();
while ($row = mysql_fetch_assoc($result))
  $names[] = $row['name'];

echo json_encode($names);

And as mysql_* functions are deprecated, consider using mysqli or PDO.

Upvotes: 5

Related Questions