Commands out of sync; you can't run this command now 错误的解决办法(php)
微wx笑
2023-01-06【运维日志】
2
0关键字:
php mysql
一个页面包含 mysqli_query和mysqli_multi_query循环执行,结果就遇到了错误:Commands out of sync; you can't run this command now
一个页面包含 mysqli_query和mysqli_multi_query循环执行,结果就遇到了错误:Commands out of sync; you can't run this command now
当使用的数据库连接是使用 mysql_connect 打开的时候,执行 mysqli_multi_query 会有警告:Warning: mysqli_multi_query() expects parameter 1 to be mysqli
解决方法
参考php官方文档 https://www.php.net/manual/zh/mysqli.multi-query.php
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$query = "SELECT CURRENT_USER();";
$query .= "SELECT Name FROM City ORDER BY ID LIMIT 20, 5";
/* 批量执行查询 */
mysqli_multi_query($link, $query);
do {
/* store the result set in PHP */
if ($result = mysqli_store_result($link)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
}
/* print divider */
if (mysqli_more_results($link)) {
printf("-----------------\n");
}else{
break;
}
} while (mysqli_next_result($link));另外,不只这一种情况会遇到这个错误,
参考:有关“Commands out of sync; you can't run this command now”错误的解决办法(php)
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/service/2023-01-06/1643.html



