好的,我正在使用预备声明来获取所有城市。
这是我的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%'
知道我做错了吗?
答案 0 :(得分:0)
好吧,我忘了在脚本结束时回复我的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;
&GT;