I was wondering if there was an easy way of making a timer in a database with PHP , I want to make it so after 30 minutes of the row in the database being created, it gets deleted. Im sorry if this isnt specific enough but I cant see how I could put any more detail in this, feel free to comment so I can elaborate in any means necessary
2
-
2You would need to run a cron task to achieve that.Darren– Darren2015-01-12 01:22:08 +00:00Commented Jan 12, 2015 at 1:22
-
Have you had a look at the mySQL trigger and event options (dev.mysql.com/doc/refman/5.7/en/create-trigger.html and dev.mysql.com/doc/refman/5.7/en/create-event.html)?user2417483– user24174832015-01-12 01:32:28 +00:00Commented Jan 12, 2015 at 1:32
Add a comment
|
1 Answer
You can make use of MySQL Events if you're on MySQL 5.1.6 or later.
Something along the lines of
CREATE EVENT purge_old_records
ON SCHEDULE EVERY 1 MINUTE
DO
DELETE FROM table1 WHERE created_at <= NOW() - INTERVAL 30 MINUTE;
should do it.
Make sure that the event scheduler is enabled by using SHOW PROCESSLIST. If it's ON you should see a process "Daemon" by user "event_scheduler".
Use SET GLOBAL event_scheduler = ON;to enable the scheduler if it's currently disabled.
More on configuring the event scheduler read here.