1

我尝试使用 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;
   }
4

1 回答 1

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))){

您可能还会发现此答案信息丰富:严格标准:mysqli_next_result() error with mysqli_multi_query

于 2016-11-15T14:14:47.183 回答