【问题标题】:mysqli multi_query and insert_id gets me Commands out of sync;mysqli multi_query 和 insert_id 让我的命令不同步;
【发布时间】:2012-05-06 05:40:21
【问题描述】:

我知道有人问过类似的问题,我已经尝试了很多(包括 php 文档),但我似乎无法解决这个问题。

我有以下几点:

$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-&gt;more_results() 返回 true,但如果我随后调用 use_result() 或 store_result() 这些返回 null。我在调用 next_result() 之前和之后都尝试过这样做,它总是为空:

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();

}

请帮助,在执行 multi_query 插入和调用 insert_id 之后如何摆脱“命令不同步;您现在无法运行此命令”。谢谢。

【问题讨论】:

    标签: php mysqli multi-query insert-id


    【解决方案1】:
    $values = ('v1' => '','v2' => '','v3' => '');
    foreach ($values as $key => $val) {
      $query = "insert into page title values ('$key')";
      $mysqlidb->query($query);
      $values[$key] = $mysqlidb->insert_id;
    }
    

    【讨论】:

    • 谢谢,但是如果我用多个(单个)查询替换我的 multi_query 不会对性能产生影响吗?
    • 对,我已经尝试过你的解决方案,我会用这个。谢谢。
    猜你喜欢
    • 2011-06-15
    • 2015-10-31
    • 1970-01-01
    • 2012-01-25
    • 2011-08-16
    • 2011-07-27
    • 2017-08-28
    • 2018-09-22
    • 1970-01-01
    相关资源
    最近更新 更多