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

admin2025-06-02  0

I am inserting data in the custom table using the $wpdb->insert. However, upon error I don't seem to get any information from $wpdb->last_error. What could be the cause of this?

I already have this configuration set in wp-config

define('WP_DEBUG', false);
define('SAVEQUERIES', true);

The code is as below:

$result = $wpdb->insert($this->table, $data_);
if (false === $result) {
   error_log($wpdb->last_error);
}

$this->table and $data_ are populated correctly as it runs successfully. But I don't seem to get any information on the query that is failed. Is there any way I can get the actual query ran in $wpdb->insert?

Solution

OK so I figured out the problem. Insert query was failing because one of the column data was huge and exceeding the column size of the database. The column was varchar(500) but the actual data was more then 500 characters, thus failing the query. I changed the column to TEXT and insert was successful without errors.

I am inserting data in the custom table using the $wpdb->insert. However, upon error I don't seem to get any information from $wpdb->last_error. What could be the cause of this?

I already have this configuration set in wp-config

define('WP_DEBUG', false);
define('SAVEQUERIES', true);

The code is as below:

$result = $wpdb->insert($this->table, $data_);
if (false === $result) {
   error_log($wpdb->last_error);
}

$this->table and $data_ are populated correctly as it runs successfully. But I don't seem to get any information on the query that is failed. Is there any way I can get the actual query ran in $wpdb->insert?

Solution

OK so I figured out the problem. Insert query was failing because one of the column data was huge and exceeding the column size of the database. The column was varchar(500) but the actual data was more then 500 characters, thus failing the query. I changed the column to TEXT and insert was successful without errors.

Share Improve this question edited Jan 23, 2018 at 20:36 Nathan Johnson 6,5386 gold badges30 silver badges49 bronze badges asked Apr 29, 2016 at 10:22 Ghazanfar MirGhazanfar Mir 1631 gold badge2 silver badges10 bronze badges 1
  • Same problem as you, as your solution worked. Thanks! – Allen Gingrich Commented Jul 23, 2019 at 17:23
Add a comment  | 

3 Answers 3

Reset to default 5

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).

https://core.trac.wordpress/ticket/32315

One of the columns inputs may be larger than the column is. Wordpress detects this and will not even send the query to the DB.

The Diff there shows a patch for wp-db you can put in to get more information in the last_error message so it will not be empty.

try this

$wpdb->show_errors();
$result = $wpdb->insert($this->table, $data_);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748824532a314029.html

最新回复(0)