Вопрос

I know similar questions have been asked, I've tried a bunch of them (including the php docs) but I don't seem to get this to work.

I have the following:

$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
}

If I execute the above code, no matter what I try to do next on the db I get "Commands out of sync; you can't run this command now" error.

I understand that I have to close all results, the issue is I don't know how.

It seems there are results because $mysqli->more_results() returns true but if I then call use_result() or store_result() these return null. I've tried doing that before and after calling next_result() and it's always null:

while($mysqli->more_results()) {
    //next line returns error, calling close() on null:
  $mysqli->use_result()->close();
  $mysqli->next_result();
}

or

while($mysqli->more_results()) {
  $mysqli->next_result();
  //next line returns error, calling close() on null:
  $mysqli->use_result()->close();

}

Please help, how to get rid of "Commands out of sync; you can't run this command now" after doing multi_query inserts and calling insert_id. Thanks.

Это было полезно?

Решение

$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 с атрибуция
Не связан с StackOverflow
scroll top