2

May I know If it is possible?
Updating MySQL Database without using button, I mean something like script.

Scenario:
I want to have running script that will search for a value that will meet the condition I want then it will automatically update the value.

Example:

I have the columns named - dateStarted, dateExpiration and status so if dateExpiration is today it will change the value of status from ACTIVE to INACTIVE.

PS: dateStarted + 30 days = dateExpiration

This is my ideal codes so far but don't know how I will call the data and update it without using button something like it must be running in background.

$dateExpired = date('Y-m-d', strtotime($dateStart. ' + 30 days'));
$dateExpired2 = date('Y-m-d', strtotime($dateStart. ' + 90days'));

$status = "FRESH";

if(strtotime($dateStart)<strtotime('-30 days')){
$status = "INACTIVE";
}

if(strtotime($dateStart)<strtotime('-90 days')){
$status = "DORMANT";
}

Thank you.

Community
  • 1
  • 1
Edmhar
  • 642
  • 1
  • 7
  • 24

1 Answers1

2

In order to automatically update a database based on an event, you can use a MySQL trigger.

Example from the manual:

mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
    -> FOR EACH ROW
    -> BEGIN
    ->     IF NEW.amount < 0 THEN
    ->         SET NEW.amount = 0;
    ->     ELSEIF NEW.amount > 100 THEN
    ->         SET NEW.amount = 100;
    ->     END IF;
    -> END;//
mysql> delimiter ;

Reference:

The trigger first needs to be create inside an SQL control panel.

If you wish to run a cron job, which may be an option for you, consult the following:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141