14

$wpdb->insert() returns false which I've learned means that the insert failed. Now, I would like to know why it fails with the insert.

According to this ticket https://core.trac.wordpress.org/ticket/32315 the problem could be that the value is either too long or contains bad characters.

Here is the insert query:

$result = $wpdb->insert('table', $ins_args, array('%d', '%d', '%s', '%s', '%s', '%s'));

It's difficult to show the $ins_args array values since some values are pretty long. Specially the one for field named value. But I use type longtext for that field. And this insert is used a lot. And most times it works with success. So it really feels like a encoding or size problem.

How do I get to know what the problem is? $wpdb->last_error is empty

10
  • Check $wpdb->last_error for inserting error Aug 23, 2017 at 12:06
  • @Anton Lukin Yea sorry should have told that. Have of course tried that but it's just empty. Aug 23, 2017 at 12:14
  • Try to debug it. Use $wpdb->last_query after inserting, then make this query via mysql client to see the error. Is it possible? Aug 23, 2017 at 12:17
  • @Anton Lukin Even though the last query is an insert(), the $wpdb->last_query right after shows an SELECT * FROM ....-query. Aug 23, 2017 at 12:31
  • We can win it :) add savequeries in your wp-config.php wordpress.stackexchange.com/a/110270/126253 Aug 23, 2017 at 12:34

2 Answers 2

15

$wpdb->insert() method returns false if the row could not be inserted. Otherwise, it returns the number of affected rows (which will always be 1).

You can turn error echoing on and off with the show_errors and hide_errors, respectively.

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->hide_errors(); ?> 

You can also print the error (if any) generated by the most recent query with print_error.

<?php $wpdb->print_error(); ?> 

You can also use $last_error field, which will contain the most recent error text generated by MySQL.

3
  • 1
    Would be good to see an example of the $last_error field. Is it just $wpdb->last_error?
    – Simon East
    Jan 23, 2019 at 5:07
  • 4
    Yes, exactly $wpdb->last_error Jan 23, 2019 at 7:01
  • Note that, if a write is aborted because a column is too long, no error is returned. That is, $wpdb->last_error` will be empty, and neither $wpdb->show_errors() and $wpdb->hide_errors() will help. See my answer below for a way to change this behaviour.
    – Brian C
    Aug 28, 2020 at 8:40
4

Sadly, wp-db.php returns false with no error, if the data you are trying to write to a column is too long to fit. This becomes a nightmare to debug without explicitly checking each column's length (and it's hard to know the lengths, though wp-db.php does provide internal functions).

I've created a tiny drop-in override which modifies wpdb behaviour to return an error in this scenario. To install it, you simply copy and paste it into your WordPress wp-content/db.php file. It overrides only a single, short, wpdb method (process_fields()) to add an error message identifying the column that is too long. Note that db.php will survive core upgrades, and you can simply remove the wp-content/db.php file at any time to revert to pure core wpdb.

Although I use this myself, as usual, test before use and usage is at your own peril.

The error code is originally taken from an idea by Liam Murphy here.

Gist for drop in db.php here

2

Your Answer

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

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