虽然pipodesign纠正了$ querystring中的错误并缓解了该问题,但未提供有关Strict Standards错误的实际解决方案。
我不同意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 errorsCumulative Affected Rows: 2// if error on second queryError: [something]Cumulative Affected Rows: 1// if error on first queryError: [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>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value}//if you want to use the snippet again...$mysqli_error=null; // clear variablesreset($queries); // reset pointer
重塑Wheel“ WHILE {}”语法 (…对于那些不喜欢测试后循环的人):
while((isset($multi_query) && (next($queries) && mysqli_more_results($mysqli) && mysqli_next_result($mysqli))) || (!isset($multi_query) && $multi_query=mysqli_multi_query($mysqli,implode(';',$queries)))){ echo "<br><br>",key($queries),": ",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>",key($queries),": ",current($queries),"Syntax Error:<br>$mysqli_error"; // display array pointer key:value}//if you want to use the snippet again...$multi_query=$mysqli_error=null; // clear variablesreset($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 = 01: INSERT INTO `TEST` (Col) VALUES ('string1'),('string2')Rows = 22: SELECt * FROM `TEST`Col = string1Col = string2Rows = 23: DELETe FROM `TEST` WHERe Col LIKE 'string%'Rows = 2
根据您的需求修改我的代码段。如果发现错误,请发表评论。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)