-
-
Notifications
You must be signed in to change notification settings - Fork 144
Description
Version: ^4.2
Bug Description
I ran into "Commands out of sync; you can't run this command now" when calling multiple stored procedures subsequently using the mysqli driver.
The exactly same issue is mentioned e.g. here #64
Steps To Reproduce
dibi::query('call upsert_products_by_source(%s)', $source);
dibi::query('call disable_inactive_products_by_source(%s)', $source);
dibi::query('call update_brand_options_by_source(%s)', $source);
dibi::query('call update_material_options_by_source(%s)', $source);
dibi::query('call update_size_options_by_source(%s)', $source);
Expected Behavior
No matter whether it's a stored procedure call or just a regular query, there shouldn't be any issues.
Possible Solution
I haven't spent too much time on this issue as I'm quite busy, but this seems to be an issue that people run into a lot.
PHP Commands Out of Sync error - https://stackoverflow.com/questions/14554517/php-commands-out-of-sync-error
Another thread - https://www.php.net/manual/en/mysqli.query.php#102904
Not very well tested, but this solves at least my issues for now.
All you have to do is make a few changes to dibi/dibi/src/Dibi/Drivers/MySqliDriver.php
(original)
public function query(string $sql): ?Dibi\ResultDriver
{
$res = @$this->connection->query($sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
if ($code = mysqli_errno($this->connection)) {
throw static::createException(mysqli_error($this->connection), $code, $sql);
} elseif ($res instanceof \mysqli_result) {
return $this->createResultDriver($res);
}
return null;
}
(improved)
public function query(string $sql): ?Dibi\ResultDriver
{
$res = @$this->connection->query($sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
if ($code = mysqli_errno($this->connection)) {
throw static::createException(mysqli_error($this->connection), $code, $sql);
} elseif ($res instanceof \mysqli_result) {
$result = $this->createResultDriver($res);
@$this->connection->next_result();
return $result;
}
return null;
}