我尝试使用db进行自动完成搜索的结果。 我使用这段代码来获取名字, 在db中,我有一行包含四列,可以包含搜索的关键字。
function search_results($conn,$str){
$sql = "SELECT g_custom_1 as str FROM gallery WHERE g_custom_1 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_2 as str FROM gallery WHERE g_custom_2 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_3 as str FROM gallery WHERE g_custom_3 LIKE '%{$str}%'";
$sql .= "SELECT g_custom_4 as str FROM gallery WHERE g_custom_4 LIKE '%{$str}%'";
//$sub_data=array();
if (mysqli_multi_query($conn,$sql))
{
$data=array();
do
{
// Store first result set
if ($result=mysqli_store_result($conn)) {
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$data[]=$row['str'];
}
// Free result set
mysqli_free_result($result);
}
}
while (mysqli_next_result($conn));
}
else{
echo "error";
}
$data_unique=array_unique($data);
return $data_unique;
}
答案 0 :(得分:0)
您需要使用分号分隔每个查询。这是您当前的字符串:
$sql="SELECT g_custom_1 as str FROM gallery WHERE g_custom_1 LIKE '%{$str}%'SELECT g_custom_2 as str FROM gallery WHERE g_custom_2 LIKE '%{$str}%'SELECT g_custom_3 as str FROM gallery WHERE g_custom_3 LIKE '%{$str}%'SELECT g_custom_4 as str FROM gallery WHERE g_custom_4 LIKE '%{$str}%'";
注意后续查询中的SELECT如何被粉碎到上一个查询的后端?
我可能会建议:
$sql[]="SELECT g_custom_1 as str FROM gallery WHERE g_custom_1 LIKE '%{$str}%'";
$sql[]="SELECT g_custom_2 as str FROM gallery WHERE g_custom_2 LIKE '%{$str}%'";
$sql[]="SELECT g_custom_3 as str FROM gallery WHERE g_custom_3 LIKE '%{$str}%'";
$sql[]="SELECT g_custom_4 as str FROM gallery WHERE g_custom_4 LIKE '%{$str}%'";
if(mysqli_multi_query($conn,implode(';',$sql))){
您可能还会发现这个答案内容丰富:Strict Standards: mysqli_next_result() error with mysqli_multi_query