2

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

5 Answers5

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

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
1

You can Use Event Scheduler in mysql

Vivek S
  • 2,010
  • 1
  • 13
  • 15
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