From 4b1aa8920630e7897706759ae47e0afed8328523 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 10 Nov 2014 05:39:50 +0000 Subject: [PATCH] `wpdb::flush()` was not flushing results correctly when using mysqli. This change also allows stored procedures or queries made with `mysqli_multi_query()` to be flushed. Includes unit tests. Fixes #28155. Props soulseekah. git-svn-id: https://develop.svn.wordpress.org/trunk@30297 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 14 +++++++++----- tests/phpunit/tests/db.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) 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 ); + } }