2

how to get results of a row with NULL element? I have to update the row which contains only the post_id and the repeat value, 'start' and 'end' should be NULL

mysql table screenshot: https://i.stack.imgur.com/r6pmA.png

$wpdb->update( $schedule_table, array( 'rpt' => $event_tag ), array( 'post_id' => $post_ID, 'start' => NULL, 'end' => NULL ), array( '%s' ) );
$wpdb->print_error();

I get this error:

WordPress database error: [] UPDATE wp_MW_3_schedule SET rpt = 'monday' WHERE post_id = 357 AND start = '' AND end = ''

0

3 Answers 3

2

My simple and quick solution is the use of a normal $wpdb->query() function:

$wpdb->query( $wpdb->prepare("UPDATE $schedule_table SET rpt = %s WHERE post_id = %d AND start IS NULL AND end IS NULL", $event_tag, $post_ID ) );
1

According to this ticket's comment, NULL format is now working correctly and you can write your query as:

$wpdb->update(
    $schedule_table, 
    array('rpt' => $event_tag), 
    array('post_id' => $post_ID, 'start' => NULL, 'end' => NULL), 
    array('%s'), 
    array('%d', NULL, NULL) 
);
0

NULL is a php construct with special meaning, you should have probably used "NULL" instead.

$wpdb->update( $schedule_table, array( 'rpt' => $event_tag ), array( 'post_id' => $post_ID, 'start' => 'NULL', 'end' => 'NULL' ), array( '%s' ) );
6
  • No, it does not work either. Dec 22, 2012 at 9:41
  • interesting, do you know what is the actual query being sent? Dec 22, 2012 at 10:17
  • It's: WordPress database error: [] UPDATE wp_MW_3_schedule SET rpt = 'monday' WHERE post_id = 357 AND start = 'NULL' AND end = 'NULL' Dec 22, 2012 at 10:19
  • maybe WordPress tries to sanitize and adds the "'" around NULL. If that is the case then your your use of query is probably the only way to do it Dec 22, 2012 at 11:41
  • 1
    just for reference there is a trac ticket for that core.trac.wordpress.org/ticket/12819 Dec 23, 2012 at 12:04

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.