简体   繁体   English

自动完成功能不适用于mysql

[英]Autocomplete doesn't work with mysql

Okay I am using prepared statement to get all the cities. 好吧,我正在使用准备好的声明来获取所有城市。

this is my php file 这是我的PHP文件

<?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 这是我的脚本和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 好吧,我忘了在脚本末尾回显我的json

<?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;

?> ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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