[英]In PHP, MySQL throws Commands out of sync error, while trying to use '$mysqli->multi_query'
[英]mysqli multi_query and insert_id gets me Commands out of sync;
我知道有人问过类似的问题,我已经尝试了很多(包括php docs),但似乎无法解决这个问题。
我有以下几点:
$query = "insert into page title values ('v1');insert into page title values ('v2');insert into page title values ('v3');"
$mysqlidb->multi_query($query);
$index = 0;
$insertId = $this->db->insert_id;
for($i = $insertId; $i < $insertId + sizeof($pages); $i++) {
//store ids in some array
}
如果执行以上代码,无论我接下来在数据库上尝试做什么,都会收到“命令不同步;您现在不能运行此命令”错误。
我知道我必须关闭所有结果,问题是我不知道如何。
似乎有结果,因为$mysqli->more_results()
返回true,但是如果我再调用use_result()或store_result(),则这些返回null。 我在调用next_result()之前和之后都尝试过这样做,但它始终为null:
while($mysqli->more_results()) {
//next line returns error, calling close() on null:
$mysqli->use_result()->close();
$mysqli->next_result();
}
要么
while($mysqli->more_results()) {
$mysqli->next_result();
//next line returns error, calling close() on null:
$mysqli->use_result()->close();
}
请执行多查询插入并调用insert_id之后,如何摆脱“命令不同步;您现在不能运行此命令”。 谢谢。
$values = ('v1' => '','v2' => '','v3' => '');
foreach ($values as $key => $val) {
$query = "insert into page title values ('$key')";
$mysqlidb->query($query);
$values[$key] = $mysqlidb->insert_id;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.