简体   繁体   English

php mysqli_multi_query没有获取数据

[英]php mysqli_multi_query not getting data

I try results from db for autocomplete search. 我尝试从db进行自动完成搜索的结果。 I use this code for getting names, in db I have a row with four columns that can contain the searched keyword. 我使用此代码获取名称,在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;
   }

You need to separate each query using a semicolon. 您需要使用分号分隔每个查询。 Here is your current string: 这是您当前的字符串:

$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}%'";

Notice how the SELECT in the subsequent queries are smashed into the rear end of the previous query? 注意后续查询中的SELECT如何被粉碎到上一个查询的后端?

I might suggest: 我可能会建议:

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

You might also find this answer informative: Strict Standards: mysqli_next_result() error with mysqli_multi_query 您可能还会发现此答案提供信息: 严格标准:mysqli_next_result()错误与mysqli_multi_query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM