From 997201088cc24e26329b64dfe8d4a43b0f7f709f Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 12 Nov 2026 23:48:25 +0100 Subject: [PATCH 1/2] Consume the remaining result sets in MysqliStatement::closeCursor() A CALL of a stored procedure that returns rows produces several result sets. closeCursor() only freed the first one, so the connection stayed busy and the next statement failed with "2014 Commands out of sync" until the connection was closed. The remaining result sets are now consumed. Co-Authored-By: Claude Opus 5.5 --- Tests/Mysqli/MysqliDriverTest.php | 15 +++++++++++++++ src/Mysqli/MysqliStatement.php | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/Tests/Mysqli/MysqliDriverTest.php b/Tests/Mysqli/MysqliDriverTest.php index adaaa3ae..f4b1d03b 100644 --- a/Tests/Mysqli/MysqliDriverTest.php +++ b/Tests/Mysqli/MysqliDriverTest.php @@ -296,6 +296,21 @@ public function testGetConnectionCollation() ); } + #[TestDox('Queries can be run after calling a stored procedure that returns rows')] + public function testQueryAfterCallingStoredProcedure() + { + static::$connection->setQuery('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc'))->execute(); + static::$connection->setQuery('CREATE PROCEDURE ' . static::$connection->quoteName('jtest_proc') . '() SELECT 1')->execute(); + + 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 { + static::$connection->setQuery('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc'))->execute(); + } + } + #[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; } From 1863312f2b950cfdddcc246bc8b5f6cf7374fded Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Fri, 13 Nov 2026 08:33:39 +0100 Subject: [PATCH 2/2] Create the stored procedure in the test on the native connection MySQL 5.7 and MariaDB 10.4 cannot prepare CREATE/DROP PROCEDURE. Co-Authored-By: Claude Opus 5.5 --- Tests/Mysqli/MysqliDriverTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Tests/Mysqli/MysqliDriverTest.php b/Tests/Mysqli/MysqliDriverTest.php index f4b1d03b..e4c36e3e 100644 --- a/Tests/Mysqli/MysqliDriverTest.php +++ b/Tests/Mysqli/MysqliDriverTest.php @@ -299,15 +299,18 @@ public function testGetConnectionCollation() #[TestDox('Queries can be run after calling a stored procedure that returns rows')] public function testQueryAfterCallingStoredProcedure() { - static::$connection->setQuery('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc'))->execute(); - static::$connection->setQuery('CREATE PROCEDURE ' . static::$connection->quoteName('jtest_proc') . '() SELECT 1')->execute(); + // 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 { - static::$connection->setQuery('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc'))->execute(); + $native->query('DROP PROCEDURE IF EXISTS ' . static::$connection->quoteName('jtest_proc')); } }