簡體   English   中英

自動完成功能不適用於mysql

[英]Autocomplete doesn't work with mysql

好吧,我正在使用准備好的聲明來獲取所有城市。

這是我的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 .= ']';

?>

這是我的腳本和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>

但是以某種方式,當我在文本框中輸入字母時,我看不到控制台中也沒有結果,也沒有錯誤,但是當我在數據庫中運行查詢時,它給了我行

這個查詢

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

知道我做錯了什么嗎?

好吧,我忘了在腳本末尾回顯我的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