$wpdb->last_error doesn’t show the query on error

If you want the query, that’ll be $wpdb->last_query (note that you also do not need SAVEQUERIES, that’s only if you want a log of every query ($wpdb->queries)

last_error is… well, the error!

Update: Possible explanation for last_error being empty – this is the source of wpdb::query():

// If we're writing to the database, make sure the query will write safely.
if ( $this->check_current_query && ! $this->check_ascii( $query ) ) {
    $stripped_query = $this->strip_invalid_text_from_query( $query );
    // strip_invalid_text_from_query() can perform queries, so we need
    // to flush again, just to make sure everything is clear.
    $this->flush();
    if ( $stripped_query !== $query ) {
        $this->insert_id = 0;
        return false;
    }
}

// Redacted code

// Keep track of the last query for debug..
$this->last_query = $query;

$this->_do_query( $query );

// Redacted code

// If there is an error then take note of it..
if ( $this->use_mysqli ) {
    $this->last_error = mysqli_error( $this->dbh );
} else {
    $this->last_error = mysql_error( $this->dbh );
}

In other words, WordPress seems to pre-emptively check the query and will abort if it deems it will fail – in this case you get your return false but no error will be set (since it was never sent to the MySQL server).

Leave a Comment