简体   繁体   中英

Autocomplete doesn't work with mysql

Okay I am using prepared statement to get all the cities.

this is my php file

<?php
include_once '../includes/db_connect.php';
$search = $_GET['term'];
if($stmtgetstore = $mysqli->prepare("SELECT * FROM cities WHERE city LIKE '%$search%'"))
{
    //$stmtgetstore->bind_param("s",$search);
    $stmtgetstore->execute();
    $getstore = $stmtgetstore->get_result();
    $stmtgetstore->close();
}
else
{
    echo $mysqli->error;
}
$array = array();

$json = '[';
$first = true;
while($store = $getstore->fetch_assoc())
{
    if (!$first) { $json .=  ','; } else { $first = false; }
    $json .= '{"value":"'.$store['city'].'"}';
}
$json .= ']';

?>

And this is my script and html

<script type="text/javascript">
$(document).ready(function()
{
    $('#autoCity').autocomplete(
    {
        source: "scripts/search_store_by_city.php",
        minLength: 2
    })/*.data( "autocomplete" )._renderItem = function( ul, item ) 
    {
      return $( "<li></li>" )
      .data( "item.autocomplete", item )
      .append( item.city )
      .appendTo( ul );
    };*/
});
</script>




  <div class="container">

    <form action="" method="GET">
      <input type="text" id="autoCity">
    </form>

  </div>

But somehow when I enter letters in textbox I see no result coming in console and no error also but when I run query in database it gives me rows

This query

SELECT * FROM cities WHERE city LIKE '%Kara%'

Any idea what me doing wrong?

Okay I forgot to echo my json at the end of the script

<?php
include_once '../includes/db_connect.php';
$search = $_GET['term'];
if($stmtgetstore = $mysqli->prepare("SELECT * FROM cities WHERE city LIKE '%$search%'"))
{
//$stmtgetstore->bind_param("s",$search);
$stmtgetstore->execute();
$getstore = $stmtgetstore->get_result();
$stmtgetstore->close();
}
else
{
echo $mysqli->error;
}
$array = array();

$json = '[';
$first = true;
while($store = $getstore->fetch_assoc())
{
if (!$first) { $json .=  ','; } else { $first = false; }
$json .= '{"value":"'.$store['city'].'"}';
}
$json .= ']';
echo $json;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM