Popa Florin
Popa Florin

Reputation: 41

PHP/MYSQL Delete data when expire

I have a custom classifieds website, and in mySQL I have date_created and date_expire. I want to make a script that will delete the ads when they are expired. The mySQL table is called: "ads" and has these columns: "id" "name" "email" "ad_headline" "date_created" "data_expirare" (this is the date when the ad will expire)

Here is what I've tried: http://pastebin.com/gD56BAWY

Upvotes: 4

Views: 2863

Answers (4)

peater
peater

Reputation: 1273

Seems like this should be automated. Take a look at this article on the MySQL Event system and using it to delete expired data: http://mrjoelkemp.com/2011/04/mysql-event-to-remove-expired-data/ Basically you can just take the best DELETE script from the previous suggestions and have it execute periodically. In his example it is run every minute, but you could probably set this to something more sporadic.

Here is the relevant link to the documentation: http://dev.mysql.com/doc/refman/5.1/en/events.html

Upvotes: 1

Andrej  Bestuzhev
Andrej Bestuzhev

Reputation: 674

delete from ads where data_expirare < NOW();

execute it when you need.

Upvotes: 4

xRobot
xRobot

Reputation: 26567

you could use CRON to do

delete * from ads where data_expirare > NOW();

every day for example.

Upvotes: -6

Chris Forrence
Chris Forrence

Reputation: 10094

What you'd want to do is run a cron job every day or so (0 0 * * *). You'd create a PHP file in this case, then within it, run the following query:

DELETE FROM ads WHERE data_expirare < NOW();

Upvotes: 5

Related Questions