4

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

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

  • Would be good to see an example of the $last_error field. Is it just $wpdb->last_error? – Simon East Jan 23 at 5:07
  • Yes, exactly $wpdb->last_error – Krzysiek Dróżdż Jan 23 at 7:01

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.