8

$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

  • Check $wpdb->last_error for inserting error – Anton Lukin Aug 23 '17 at 12:06
  • @Anton Lukin Yea sorry should have told that. Have of course tried that but it's just empty. – Peter Westerlund Aug 23 '17 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? – Anton Lukin Aug 23 '17 at 12:17
  • @Anton Lukin Even though the last query is an insert(), the $wpdb->last_query right after shows an SELECT * FROM ....-query. – Peter Westerlund Aug 23 '17 at 12:31
  • We can win it :) add savequeries in your wp-config.php wordpress.stackexchange.com/a/110270/126253 – Anton Lukin Aug 23 '17 at 12:34
5

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

| improve this answer | |
  • Would be good to see an example of the $last_error field. Is it just $wpdb->last_error? – Simon East Jan 23 '19 at 5:07
  • Yes, exactly $wpdb->last_error – Krzysiek Dróżdż Jan 23 '19 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 at 8:40
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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