Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto delete a record in table when date is expired?

Tags:

php

mysql

I would like to know how can I auto delete a record when date is expired, I'm creating a Air-ticket booking website. I need to delete from my mysql database all the expired flight details. I read somewhere that I can use cron but I have no idea how to do it. Any Help for the script will be very helpful.

like image 439
YathuGulan Avatar asked Aug 12 '13 07:08

YathuGulan


2 Answers

You may try to use MySQL Events for that:

CREATE EVENT IF NOT EXISTS `dbName`.`eventName`
ON SCHEDULE
    EVERY 1 DAY -- or 1 HOUR
COMMENT 'Description'
DO
    BEGIN
    
    DELETE FROM `dbName`.`TableName` WHERE `DateCol` < NOW();
    
    END

NOTE that MySQL Event Scheduler need to be enabled on your server:

SET GLOBAL event_scheduler = ON;

More info here.

like image 67
BlitZ Avatar answered Oct 31 '22 16:10

BlitZ


Deleting records is not a good idea, I will suggest you Add ActiveFrom and ActiveTo DateTime Column in your Details table then do not display the expired records in your front end.

like image 1
January Mmako Avatar answered Oct 31 '22 16:10

January Mmako