Auto Trigger a Mysql query every day at specific time - php

I want to give my users bonus every day at 00:01 AM, so i am looking for an automatic trigger in mysql to update Bonus in users.
UPDATE users SET bonus = bonus + 10
How can i do this ?
Thank you

MySQL has a built-in scheduler that can be used to execute tasks according to a given schedule.
Here is how to create a scheduled event for your use case:
create event grant_user_bonus_daily
on schedule every 1 day
starts current_date + interval 1 day + interval 1 minute -- starts tomorrow at 00:01
do
update users set bonus = bonus + 10;

SET GLOBAL event_scheduler = ON;
Example:
CREATE EVENT test_event_02
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL Event 2',NOW());
Full tutorial here
Official MySQL link

Related

How to increment mysql data after a fixed time slot automatically?

For example, I am creating a student management system and I want to increase the student standard after every year. If student is admitted in 1st standard then next year he would be in 2nd after that 3rd and so on.. Please help!
you can use MySQL EVENT like
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 YEAR
DO
UPDATE `student` SET `standard` = `standard` + 1
WHERE some_ID_Column = value;
Set up a cron job that runs nightly and run a stored procedure that checks if the students have reached the condition for the next standard and if they have update the records.

How database server can automatically update

I am making a game and i want to do this:
every 30 minutes every player must gain coins (online players and offline players too!).
So there will be a way that the database will automatically update the columns after 30 minutes and will add to every player some coins.
You would want to create an event like so
CREATE EVENT addcoins
ON SCHEDULE AT '2014-11-12 12:00:00' + INTERVAL 30 MINUTE
DO
UPDATE `player_tbl` SET `player_tbl`.`coins` = `player_tbl`.`coins` + 1;
An alternative is to use a cronjob - you can create a script (e.g. in PHP) on your server then create a cronjob to execute the script every 30 minutes.

update column if date is older than 30 days

I have a table where I have column name 'date' and another column with the name 'status' if I use a cron job to run everyday and check if the date is older than 30 day then to change the status to expired, what code do I need to run using mysqli and php.
MySQL event scheduler is more better.
Start MySQL event scheduler by executing following query in PhpMyAdmin or MySQL command prompt.
Enter this.
SET GLOBAL event_scheduler = 1;
This will update your table.
CREATE EVENT newEvent
ON SCHEDULE EVERY 1 DAY
DO
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY;
Try this:
UPDATE table SET status="expired" where YOURDATEFIELD < date_sub(now(), interval 1 month);
Use INTERVAL in your UPDATE query..
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY

Insert data on 1st day of every month only once

I want to insert data into a table on 1st day of every month. And it has to be just for one time. I mean one row for each 1st day of month. I am doing all this in a PHP file and using mySql.
So far i got this much-
if(Date('j')==1)
{
$query = select 1 from table where extract (year from t1) = extract(year from now()) and extract(month from t1) = extract(month from now()) LIMIT 1
if (#mysql_num_rows(mysql_query($query)) == 0)
{
//perform insert operation
}
}
I haven't tried this query till yet.
Maybe this will do the trick
SELECT * FROM xxx WHERE (DATE_FORMAT(CURDATE() ,'%d')==1)
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE())-1,0) as StartOfLastMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfThisMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfNextMonth
Try out below event scheduler
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO
"YOUR QUERY"
Your have to run this on the date on which you want to run on 1 month interval.
You get more information on http://dev.mysql.com/doc/refman/5.1/ja/create-event.html
You have two solutions, depending on what you exactly want.
If your data must be inserted at the first day: the only way is to insert it using a cron job which run at the correct time. You can use the mysql scheduler to do the job if it's only mysql related.
If your data need to be in the table after the first day, but you don't care if it is added in realtime or only when you access that data then you don't need a cron job. You can check for that data when you access it, then insert it before reading if it's not already present.
you can achieve with mysql event scheduler--
http://dev.mysql.com/doc/refman/5.1/en/events.html
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);
OR for Stored Procedure.
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();

How to auto update a record in database?

I have a jobs table in which I inserts new jobs.
This table also contains the job post date.
By default the job status is open when a new insertion take place.
Now I want to change the status of the job from open to close when the jobs becomes older than 30 days.
How will I do this?
Try creating a event which runs every day like below
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
-- Update Changed syntax
CREATE EVENT myevent
ON SCHEDULE EVERY 24 HOUR
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
Use a cron job to handle this. http://en.wikipedia.org/wiki/Cron
If you can't handle crons, you could do a "poor man's cron". Means the actual process that updates all of your jobs records takes place when someone visits your page (with additional checks when the last run was). If you're doing it the "poor" way I suggest to fire off another thread to keep your site responsible.

Categories