【发布时间】:2013-09-14 16:05:03
【问题描述】:
我正在使用自动完成,其中建议来自数据库并且工作正常,但是我尝试将其更改为 mysqli 但它不起作用。它没有显示任何建议;没有错误。
- 我在 mysqli 端缺少什么?
- 如何才能添加更多表(我有 40 个表)?谢谢。
MySQL:
<?php
mysql_connect("localhost","root","");
mysql_select_db("database");
$term=$_GET["term"];
$query=mysql_query("SELECT * FROM products1 where title like '%".$term."%' order by id ");
$json=array();
while($student=mysql_fetch_array($query)){
$json[]=array(
'value'=> $student["title"],
'label'=>$student["title"]
);
}
echo json_encode($json);
?>
我对 MySQLi 准备好的语句的尝试:
<?php
$mydb = new mysqli('localhost', 'root', '', 'database');
$q = $_POST['term'];
$stmt = $mydb->prepare(" SELECT * from products1 where title LIKE ? ");
echo $mydb->error;
$stmt->bind_param('s', $q);
$stmt->execute();
?>
<?php
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$json[]=array(
'value'=> $student["title"],
'label'=>$student["title"]
);
}
echo json_encode($json);
?>
【问题讨论】:
-
您可能只需要在搜索词周围加上
%。$q = '%'.$_POST['term'].'%';stackoverflow.com/questions/15229880/…
标签: php mysql autocomplete mysqli