php – 严格标准:mysqli_next_result()错误与mysqli_multi_query

前端之家收集整理的这篇文章主要介绍了php – 严格标准:mysqli_next_result()错误与mysqli_multi_query前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试过使用multi_query但是我一直在弹出严格的标准消息.
$querystring = "INSERT INTO responses VALUES('1','2','3','4'); INSERT INTO responses VALUES('1','4')";

if (MysqLi_multi_query($db,$querystring)) {
   do {
       if ($result = MysqLi_store_result($db)) {
           //
       }
   } while (MysqLi_next_result($db));
}
echo "end";

我得到的错误信息是:

Strict Standards: MysqLi_next_result(): There is no next result set. Please,call MysqLi_more_results()/MysqLi::more_results() to check whether to call this function/method

我试过添加删除 – ;但没有运气.

虽然pipodesign纠正了$querystring中的错误并缓解了问题,但没有提供有关严格标准错误的实际解决方案.

我不同意SirBT的建议,从DO WHILE改为WHILE是没有必要的.

您收到的严格标准消息非常有用.
要服从,请使用:

do{} while(MysqLi_more_results($db) && MysqLi_next_result($db));

然后,您无需在循环内部编写条件退出或中断,因为while条件将在第一次出现错误时中断循环. *注意,如果第一个查询错误,do-while之前的if语句将拒绝进入循环.

在您的示例中,您只运行INSERT查询,因此您将不会收到任何要处理的结果集.如果要计算已添加的行数,请使用MysqLi_affected_rows().

作为您问题的完整解决方案:

if(MysqLi_multi_query($db,$querystring)){
    do{
        $cumulative_rows+=MysqLi_affected_rows($db);
    } while(MysqLi_more_results($db) && MysqLi_next_result($db));
}
if($error_mess=MysqLi_error($db)){echo "Error: $error_mess";}
echo "Cumulative Affected Rows: $cumulative_rows";

输出

// if no errors
Cumulative Affected Rows: 2

// if error on second query
Error: [something]
Cumulative Affected Rows: 1

// if error on first query
Error: [something]
Cumulative Affected Rows: 0

晚编辑:

由于MysqLi的新手在这篇文章中遇到了绊脚石,我将提供一个通用但强大的代码片段,使用multi_query()处理带/不带结果集的查询,并添加一个功能显示正在处理数组中的哪个查询

经典“IF(){DO {} WHILE}”语法:

if(MysqLi_multi_query($MysqLi,implode(';',$queries))){
    do{
        echo "<br><br>",key($queries),": ",current($queries);  // display key:value @ pointer
        if($result=MysqLi_store_result($MysqLi)){   // if a result set
            while($rows=MysqLi_fetch_assoc($result)){
                echo "<br>Col = {$rows["Col"]}";
            }
            MysqLi_free_result($result);
        }
        echo "<br>Rows = ",MysqLi_affected_rows($MysqLi); // acts like num_rows on SELECTs
    } while(next($queries) && MysqLi_more_results($MysqLi) && MysqLi_next_result($MysqLi));
}
if($MysqLi_error=MysqLi_error($MysqLi)){
    echo "<br><br>",current($queries),"Syntax Error:<br>$MysqLi_error";  // display array pointer key:value
}
//if you want to use the snippet again...
$MysqLi_error=null; // clear variables
reset($queries); // reset pointer

重新发明轮“WHILE {}”语法(…对于那些不喜欢测试后循环的人):

while((isset($multi_query) && (next($queries) && MysqLi_more_results($MysqLi) && MysqLi_next_result($MysqLi))) || (!isset($multi_query) && $multi_query=MysqLi_multi_query($MysqLi,$queries)))){
    echo "<br><br>",current($queries);  // display array pointer key:value
    if($result=MysqLi_store_result($MysqLi)){
        while($rows=MysqLi_fetch_assoc($result)){
            echo "<br>Col = {$rows["Col"]}";
        }
        MysqLi_free_result($result);
    }
    echo "<br>Rows = ",MysqLi_affected_rows($MysqLi); // acts like num_rows on SELECTs
}
if($MysqLi_error=MysqLi_error($MysqLi)){
    echo "<br><br>","Syntax Error:<br>$MysqLi_error";  // display array pointer key:value
}
//if you want to use the snippet again...
$multi_query=$MysqLi_error=null; // clear variables
reset($queries); // reset pointer

因此,给定以下查询的任一片段都将提供相同的输出

查询数组:

$queries[]="SELECT * FROM `TEST`";
$queries[]="INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')";
$queries[]="SELECT * FROM `TEST`";
$queries[]="DELETE FROM `TEST` WHERE Col LIKE 'string%'";

输出

0: SELECT * FROM `TEST`
Rows = 0

1: INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')
Rows = 2

2: SELECT * FROM `TEST`
Col = string1
Col = string2
Rows = 2

3: DELETE FROM `TEST` WHERE Col LIKE 'string%'
Rows = 2

根据您的需要修改我的代码段.如果您发现错误,请发表评论.

原文链接:https://www.f2er.com/php/136610.html

猜你在找的PHP相关文章