diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 2e8b4079fe..f01ba97a8c 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -1322,12 +1322,16 @@ class wpdb { $this->rows_affected = $this->num_rows = 0; $this->last_error = ''; - if ( is_resource( $this->result ) ) { - if ( $this->use_mysqli ) { - mysqli_free_result( $this->result ); - } else { - mysql_free_result( $this->result ); + if ( $this->use_mysqli && $this->result instanceof mysqli_result ) { + mysqli_free_result( $this->result ); + $this->result = null; + + // Clear out any results from a multi-query + while ( mysqli_more_results( $this->dbh ) ) { + mysqli_next_result( $this->dbh ); } + } else if ( is_resource( $this->result ) ) { + mysql_free_result( $this->result ); } } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 0fbd6218ba..601380234c 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -458,4 +458,36 @@ class Tests_DB extends WP_UnitTestCase { $this->assertEquals( $expected2, $wpdb->last_query ); $wpdb->suppress_errors( $suppress ); } + + /** + * mysqli_ incorrect flush and further sync issues. + * + * @ticket 28155 + */ + function test_mysqli_flush_sync() { + global $wpdb; + if ( ! $wpdb->use_mysqli ) { + $this->markTestSkipped( 'mysqli not being used' ); + } + + $suppress = $wpdb->suppress_errors( true ); + + $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' ); + $wpdb->query( 'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN + SELECT ID FROM `' . $wpdb->posts . '` LIMIT 1; + END' ); + + if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) { + $wpdb->suppress_errors( $suppress ); + $this->markTestSkipped( 'procedure could not be created (missing privileges?)' ); + } + + $this->factory->post->create(); + + $this->assertNotEmpty( $wpdb->get_results( 'CALL `test_mysqli_flush_sync_procedure`' ) ); + $this->assertNotEmpty( $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` LIMIT 1" ) ); + + $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' ); + $wpdb->suppress_errors( $suppress ); + } }