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

$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?


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.

  • Same problem as you, as your solution worked. Thanks! Jul 23, 2019 at 17:23

3 Answers 3


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

  • but why don't I still get anything in the last_error? insert query results in false which means something failed, but still doesn't contain anything in last_error? Wierd? Apr 29, 2016 at 10:45
  • Updated answer with possible explanation. Apr 29, 2016 at 10:52
  • So what is the best way to get this work? I mean always get something in last_error upon errors Apr 29, 2016 at 11:08


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.

enter image description here

  • This is truly a cool answer. I've adapted your answer into a wp-content/db.php file so it can be easily and quickly dropped in place to persist a solution across WordPress upgrades. See here for gist and more details.
    – Brian C
    Aug 28, 2020 at 8:57
  • 1
    Oh neat thanks @BrianC I saw an email a little while back that this issue has a milestone of 5.6 now. Aug 29, 2020 at 1:24
  • Looks like a core fix is proposed but missed out on 5.5 and I think 5.6. Here’s hoping, sigh.
    – Brian C
    Aug 29, 2020 at 1:25

try this

$result = $wpdb->insert($this->table, $data_);
  • Sorry, unfortunately that doesn't work. The problem is that the error is caused by wpdb code which actually just exits with false and doesn't set last_error.
    – Brian C
    Aug 28, 2020 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.