0

I want to delete data from database after every 24hours insertion.

For example: A user is added today and he is not active, after 24hours the user will be automatically deleted.

I intend doing this without cron because cron is not in my local/offline server.

Please i really need your help.

2
  • the only way you can do this with php is in the database row have a last logged on time stamp. if the user comes back after 24 hours delete the record. PHP is only run when the webpage is loaded and will not run behind the scenes Commented Feb 11, 2017 at 21:09
  • MySQL has scheduled events (see dev.mysql.com/doc/refman/5.7/en/create-event.html), you might be able to use these - log a date in the database and every day schedule something like DELETE FROM yourtable WHERE date=... Commented Feb 11, 2017 at 21:21

1 Answer 1

2

You could simply use

DELETE FROM your_table 
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))

and use the mysql scheduled events

could be somethings like this

CREATE EVENT my_dayly_event
    ON SCHEDULE
      EVERY 1 DAY
    COMMENT 'delete old values.'
    DO
    DELETE FROM your_table 
    WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))

anyway take a look a this doc. https://dev.mysql.com/doc/refman/5.7/en/create-event.html ... could be useful

Sign up to request clarification or add additional context in comments.

3 Comments

i don't know how to use the scheduler event please can you write it for me?
ok bro that means with this i will have to wait for 1 day before i see the result. right?
This is a sample ... you can study the doc and try what is better for you .. you can change the frequence ( 1 HOUR or 1 MINUTE) for debugging or test ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.