Search code examples
phpmysqldatabaseinsert

PHP - How do i delete data from database automatically after 24 hour of insertion


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.


Solution

  • 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