Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling multiple stored procedures subsequently - Commands out of sync; you can't run this command now #434

Open
N4thyra opened this issue Feb 8, 2023 · 0 comments

Comments

@N4thyra
Copy link

N4thyra commented Feb 8, 2023

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;
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant