Skip to content
Browse files

Fix some queries returning wrong number of rows.

  • Loading branch information...
1 parent ccf21b0 commit 411f128d977801a6cacde97d1314825f9a22193a @markmont committed Sep 1, 2013
Showing with 1 addition and 1 deletion.
  1. +1 −1 db.php
View
2 db.php
@@ -297,7 +297,7 @@ function query( $query ) {
// Return number of rows affected
$return_val = $this->rows_affected;
} else {
- $return_val = $this->num_rows = count( $this->last_result );
+ $return_val = $this->num_rows = count( $this->result );
}
$this->last_result = $this->dbh->get_results();

1 comment on commit 411f128

@markmont
Owner

wb-db-driver 1.5, WordPress 3.6, PHP version 5.5.1, PDO driver, MariaDB version 5.5.31.

Some queries (anything other than create, alter, truncate, drop, insert, delete, update, replace) may return the wrong number of rows. Example:

global $wpdb;
$table_name = $wbdp->prefix . 'posts';
if( ! $wpdb->query( "SHOW TABLES LIKE '{$table_name}';" ) ) {
    error_log( "Oh no, the WordPress posts table, ${table_name}, does not exist!" );
} else {
    error_log( "All is well." );
}

When run, the above code will claim that the posts table does not exist, even though it does exist. This is a real-world test case from a plugin that fails to work with wp-db-driver, the Wysija Newsletters plugin, see lines 495-497 of
http://plugins.trac.wordpress.org/browser/wysija-newsletters/trunk/helpers/install.php#495

(Note that additional changes to Wysija Newletters are needed to get it to work with wp-db-driver)

The problem is due to last_result being used on line 300 of wp-db-drivers/db.php before it gets set on line 303; the fix is straightforward.

Please sign in to comment.
Something went wrong with that request. Please try again.