2

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

8 Comments

It doesn't work quite as expected - I get some of the output correct, but some of results just show up as null.
Also - how much of a job would it be to rewrite it to use mysqli?
For the NULL, I don't know for sure about the problem. Perhaps some of the values in customer.name are NULL? Converting your code from mysql_* to mysqli should not be too difficult.
Doing the query in phpmyadmin returns no rows with the value NULL - they do however contain special danish characters (Æ,Ø,Å)
Alright, fixed that part with a htmlentities. Now I just got the problem that in my search box, Ø for example shows up as Ø and not Ø.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.