0

I'm trying to populate an input field, using PHP/MySQL, with the Drew Wilson's jQuery Autosuggest plugin found here: http://tips4php.net/2010/09/ajax-autocomplete-with-jquery-and-php/

I get this error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 21

Here is my code:

$con = mysql_connect("localhost","username","password");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$counter='0';
echo "{";
echo "query:'$query',";
echo "suggestions:[";
$res = mysql_query("SELECT airport, code FROM iata_airport_codes where name like '$query%' ORDER BY airport desc");
while($row = mysql_fetch_array($res)) {
    $counter++;
    if ($counter > 1) {
        echo ",";
    }

    $airport=$row["airport"];
    $code=$row["code"];

    echo "'$airport', ('$code')";
}
echo "],}";

mysql_close($con);

What am I missing here? Can't see what I'm doing wrong.

Thanks in advance!

1
  • What are the contents of $query, also look up SQL injection and escaping inputs. Commented Dec 14, 2011 at 13:44

2 Answers 2

2

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource on line 21

Always means that there is an error in your SQL query. Try printing myqsl_error() contents.

And you should use json_encode() not print json by yourself.

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

Comments

1

It`s display an error:

 mysql_query("SELECT airport, code FROM iata_airport_codes where name like '$query%' ORDER BY airport desc") or die(mysql_error());

Comments

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.