1

I am mysql beginner, how to delete 30 day old data using php?
This code is not working?

global $wpdb;
$table = $wpdb->prefix . "userinfo"; 
$delete_rows ="DELETE * FROM $table WHERE timeall <".strtotime('-1 month');
$wpdb->query($table, $delete_rows);

my data photo 1 enter image description here

my data photo 2 enter image description here

3
  • 1
    I don't see anything WordPress specific.
    – fuxia
    Mar 22, 2017 at 1:35
  • Looks like you're not selecting your table correctly. $wpdb->prefix . "wp_userinfo" would equate to wp_wp_userinfo. If you're using $wpdb->prefix, you don't need to add the prefix yourself. Just change to $wpdb->prefix . "userinfo"
    – danbrellis
    Mar 22, 2017 at 2:39
  • If this is your userinfo table, you've made some serious modifications to the table structure O.o
    – bosco
    Mar 24, 2017 at 2:47

1 Answer 1

1

You're adding an extra wp_ prefix to your table and your using a timestamp instead of a MYSQL datetime. Below is how WP suggests you use $wpdb->query to delete rows, taken from the codex.

EDITED

global $wpdb;

$wpdb->query(
  "DELETE FROM " . $wpdb->prefix . "userinfo
   WHERE timeall < DATE_SUB(CURDATE(),INTERVAL 30 DAY)"
);

$wpdb->prefix adds the wp_ for you.

1
  • answer updated with the correct sql query
    – danbrellis
    Mar 22, 2017 at 13:16

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