I have a mysql database with a table that has several fields including:
enter_date (which is a timestamp field – ie 2011-09-13 21:04:42)
status (which defaults to active)
period_allowed (which is a number of days ie 5 or 20 or 30 etc)
Now what I need to do is after the period_allowed (starting from the enter_date field) the status is to change from ‘active’ to ‘expired’.
ie
if enter date: 2011-09-10- 12:00:00
period_allowed: 5
Then on 15 September the status is to be AUTOMATICALLY changed from ‘active’ to ‘expired’
Ideas for best and easiest way to do this?
Thank you
P.S. Looking at setting up a cron job.. but coding to achieve this?
You can create a stored procedure to execute this update SQL, and create a MySQL Event (if using a recent version of MySQL) which runs that stored procedure periodically. Otherwise create a cron job that calls the stored procedure (or just runs the update sql) using the mysql command-line client.
See MySQL 5.1 Create Event documentation, it has a simple example for creating an event that runs an update statement. Or, to create a cron job, you can create an entry in your crontab that calls mysql using the -e flag to execute a statement.