diff --git a/Tests/Mysqli/MysqliDriverTest.php b/Tests/Mysqli/MysqliDriverTest.php index adaaa3ae..e4c36e3e 100644 --- a/Tests/Mysqli/MysqliDriverTest.php +++ b/Tests/Mysqli/MysqliDriverTest.php @@ -296,6 +296,24 @@ public function testGetConnectionCollation() ); } + #[TestDox('Queries can be run after calling a stored procedure that returns rows')] + public function testQueryAfterCallingStoredProcedure() + { + // Older servers cannot prepare these statements, so run them on the native connection + static::$connection->connect(); + $native = static::$connection->getConnection(); + $native->query('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc')); + $native->query('CREATE PROCEDURE ' . static::$connection->quoteName('jtest_proc') . '() SELECT 1'); + + try { + $this->assertEquals(1, static::$connection->setQuery('CALL ' . static::$connection->quoteName('jtest_proc') . '()')->loadResult()); + $this->assertEquals(1, static::$connection->setQuery('CALL ' . static::$connection->quoteName('jtest_proc') . '()')->loadResult()); + $this->assertEquals(2, static::$connection->setQuery('SELECT 2')->loadResult()); + } finally { + $native->query('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc')); + } + } + #[TestDox('The database connection encryption can be retrieved')] public function testGetConnectionEncryption() { diff --git a/src/Mysqli/MysqliStatement.php b/src/Mysqli/MysqliStatement.php index a2f46b5c..d6e640a8 100644 --- a/src/Mysqli/MysqliStatement.php +++ b/src/Mysqli/MysqliStatement.php @@ -330,6 +330,13 @@ private function bindValues(array $values) public function closeCursor(): void { $this->statement->free_result(); + + // A CALL of a stored procedure returns further result sets; they must be consumed, or the next statement fails + // with "Commands out of sync" + while ($this->statement->more_results() && $this->statement->next_result()) { + $this->statement->free_result(); + } + $this->result = false; }