Get error messages when $wpdb->insert() returns false?

$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

2 s
2

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

Leave a Comment