Is there any ways to execute a SQL query automatically after 30 days? Actually I want to change the status of an account from active to inactive in database without manual editing
Asked
Active
Viewed 2,535 times
2
-
Set up a cron job like described in this similar question: http://stackoverflow.com/questions/190561/what-is-the-best-method-for-scheduled-tasks-in-php – Digital Chris Dec 19 '13 at 17:12
-
use cronjobs for this – Sam Dec 19 '13 at 17:12
-
cron jobs, database triggers, all sorts of options. Check out some of the links posted. – Zarathuztra Dec 19 '13 at 17:14
-
you need to create a batch which might run in daily basis, which will call that particular script. – Nitesh Mishra Dec 19 '13 at 17:14
5 Answers
2
You need do it with cronjob, edit your /etc/crontab file to setup a new job like:
0 0 30 * * apache php /path/yourScript.php
This will be executed each day 30 of month.

Ignacio Ocampo
- 2,693
- 1
- 20
- 31
-
+1 and just an FYI to OP, you'd need to make sure you have CLI PHP installed. – Zarathuztra Dec 19 '13 at 17:14
1
MySQL 5.1.6+ has an Event Scheduler.
If you're using an earlier version, you could write a little script which executed the desired SQL, and schedule it with at, cron or similar.

Eisa Adil
- 1,743
- 11
- 16
0
CREATE EVENT e_hourly
ON SCHEDULE
EVERY 1 HOUR DO ***your update query here***

Vivek S
- 2,010
- 1
- 13
- 15
0
You have to setup a cronjob on the server, like Ignacio Ocampo said.
In your database, you have to create a column (last_activity) to track the last time the user has logged in (I recommend using unix timestamp)
Then, on the PHP script that will run the cronjob, you update the rows, for example:
PHP: $timestamp = time() - 60 * 60 * 24 * 30;
SQL: UPDATE users_table SET active = 0 WHERE last_activity < $timestamp

Gonzalingui
- 1,357
- 14
- 18