I found a autosuggest code, but the connection to database is mysqli, so I edit the syntax to mysql_connect…
Here is the original syntax:
<?php
$db = new mysqli('DB_HOST', 'USERNAME' ,'PASSWORD', 'DATABASE_NAME');
if(!$db) {
echo 'Could not connect to the database.';
} else {
if(isset($_POST['queryString'])) {
$queryString = $db->real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = $db->query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");
if($query) {
echo '<ul>';
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\\''.addslashes($result->country).'\\');">'.$result->country.'</li>';
}
echo '</ul>';
} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
}
?>
and I edit it:
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_password = '';
$db_name = 'db_upload';
$db = mysql_connect($db_host , $db_user ,$db_password, $db_name);
if(isset($_POST['queryString'])) {
$queryString = mysql_real_escape_string($_POST['queryString']);
if(strlen($queryString) >0) {
$query = mysql_query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");
if($query) {
echo '<ul>';
while ($result = mysql_fetch_object($query)) {
echo '<li onClick="fill(\\''.addslashes($result->country).'\\');">'.$result->country.'</li>';
}
echo '</ul>';
} else {
echo 'OOPS we had a problem :(';
}
} else {
// do nothing
}
} else {
echo 'There should be no direct access to this script!';
}
?>
when i run it, it was always fall in else statement
‘OOPS we had a problem :(’
even I input correct data.
I revised my code and I think I have a problem in javascript because the data did not display.
<?php
include 'config.php';
$queryString = $_POST['queryString'];
if(strlen($queryString) >0) {
$query = "SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result))
{
echo '<ul class="list">';
while($row = mysql_fetch_array($result))
{
echo '<li onClick="fill(\\''.addslashes($row->country).'\\');">'.$row->country.'</li>';
}
echo "</ul>";
}
else {
echo 'OOPS we had a problem :(';
}
?>