WordPress.org

Make WordPress Core

Changeset 30297


Ignore:
Timestamp:
11/10/2014 05:39:50 AM (6 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/wp-db.php

    r30292 r30297  
    13231323        $this->last_error  = '';
    13241324
    1325         if ( is_resource( $this->result ) ) {
    1326             if ( $this->use_mysqli ) {
    1327                 mysqli_free_result( $this->result );
    1328             } else {
    1329                 mysql_free_result( $this->result );
    1330             }
     1325        if ( $this->use_mysqli && $this->result instanceof mysqli_result ) {
     1326            mysqli_free_result( $this->result );
     1327            $this->result = null;
     1328
     1329            // Clear out any results from a multi-query
     1330            while ( mysqli_more_results( $this->dbh ) ) {
     1331                mysqli_next_result( $this->dbh );
     1332            }
     1333        } else if ( is_resource( $this->result ) ) {
     1334            mysql_free_result( $this->result );
    13311335        }
    13321336    }
  • trunk/tests/phpunit/tests/db.php

    r29701 r30297  
    459459        $wpdb->suppress_errors( $suppress );
    460460    }
     461
     462    /**
     463     * mysqli_ incorrect flush and further sync issues.
     464     *
     465     * @ticket 28155
     466     */
     467    function test_mysqli_flush_sync() {
     468        global $wpdb;
     469        if ( ! $wpdb->use_mysqli ) {
     470            $this->markTestSkipped( 'mysqli not being used' );
     471        }
     472
     473        $suppress = $wpdb->suppress_errors( true );
     474
     475        $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' );
     476        $wpdb->query( 'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN
     477            SELECT ID FROM `' . $wpdb->posts . '` LIMIT 1;
     478        END' );
     479
     480        if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) {
     481            $wpdb->suppress_errors( $suppress );
     482            $this->markTestSkipped( 'procedure could not be created (missing privileges?)' );
     483        }
     484
     485        $this->factory->post->create();
     486
     487        $this->assertNotEmpty( $wpdb->get_results( 'CALL `test_mysqli_flush_sync_procedure`' ) );
     488        $this->assertNotEmpty( $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` LIMIT 1" ) );
     489
     490        $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' );
     491        $wpdb->suppress_errors( $suppress );
     492    }
    461493}
Note: See TracChangeset for help on using the changeset viewer.