0

I have a delete query that runs when it should not, the WHERE clause condition requires that the query runs once every 600 seconds(ten minutes) since the last database update:

$delete = $wpdb->query( 
        $wpdb->prepare( 
            "
            DELETE FROM wp_counter_cookies
            WHERE UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(visit) >= %d
            ",
                600 
            )
    );

If I run the query from my script in quick succession within Wordpress, it still executes regardless of the WHERE clause specification.

When I run this PHP script outside the WordPress environment, it behaves as expected.

What may be wrong in my syntax?

  • obviously it depends on what you have in visit I don't actually see anything wordpress specific here that might have any impact – Mark Kaplun Jul 16 '16 at 16:17
  • +1 @MarkKaplun, you were absolutely right in your comment. My WordPress install time setting was late by one hour, so all my database entries were expiring as soon as created. I corrected this by changing the time settings and now all is well. – Terungwa Jul 16 '16 at 20:01
  • @Terungwa Please add this as an answer (yes, you can answer your own question :)), so that this doesn't come up in our unanswered lists. After 48 hours you will then also be able to 'accept' your answer by clicking the check mark on the left of it. Thanks! And welcome to WPSE – Tim Malone Jul 16 '16 at 20:32
0

My WordPress install time setting was late by one hour, so all my database entries were expiring as soon as created. I corrected this by changing the time settings and now all is well

  • this is because current_time is a locale relative function. therefor comparing to NOW() was comparing two different values. Im most cases it is better to just store time() in the DB and save the conversion overhead and be sure that you calculate time in exactly the same way all the time – Mark Kaplun Jul 17 '16 at 3:19

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.