php – 如何确保我捕获了MySQLi :: multi_query中的所有错误?
发布时间:2020-12-13 17:36:57 所属栏目:PHP教程 来源:网络整理
导读:docs for multi_query 说: Returns FALSE if the first statement failed. To retrieve subsequent errors from other statements you have to call mysqli_next_result() first. docs for next_result 说: Returns TRUE on success or FALSE on failure.
docs for
multi_query 说:
docs for
最后,文档中为multi_query发布的示例使用next_result的返回值来确定什么时候没有更多的查询;例如停止循环: <?php $mysqli = new mysqli("localhost","my_user","my_password","world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %sn",mysqli_connect_error()); exit(); } $query = "SELECT CURRENT_USER();"; $query .= "SELECT Name FROM City ORDER BY ID LIMIT 20,5"; /* execute multi query */ if ($mysqli->multi_query($query)) { do { /* store first result set */ if ($result = $mysqli->store_result()) { while ($row = $result->fetch_row()) { printf("%sn",$row[0]); } $result->free(); } /* print divider */ if ($mysqli->more_results()) { printf("-----------------n"); } } while ($mysqli->next_result()); // <-- HERE! } /* close connection */ $mysqli->close(); ?> 我不知道提供的查询的数量,也不知道我将要执行的SQL的任何内容.因此,我不能仅仅将查询的数量与返回的结果数进行比较.但是,如果第三个查询是断开的查询,我想向用户显示错误消息.但是我似乎没有办法告诉next_result是否失败,因为没有更多的查询要执行,或者是因为SQL语法中有错误. 如何检查所有查询的错误?
尽管文档中有代码示例,也许更好的方法是这样的:
if ($mysqli->multi_query(...)) { do { // fetch results if (!$mysqli->more_results()) { break; } if (!$mysqli->next_result()) { // report error break; } } while (true); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |