How database server can automatically update - php

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.

Related

Auto Trigger a Mysql query every day at specific time

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

How to create a CRON job and set it for every 30 minutes or hour and then cancel it after 5 intervals php

So here is what I am trying to accomplish:
User selects how often they want their post to be moved to the top of the page, whether it be every 30 minutes or every hour or every 2 hours, etc. They will be able to select how many times that it will do this. So if they select it to update every hour for 5 hours, it will then update the date/time in the database to the current time and then updates that time every hour for the next 5 hours and cancels it after the 5 hours are up.
I was thinking of running a PHP script like this to update the table since the ads are displayed by date DESC:
<?php
//cronjob.php
$id = $_SESSION['ad_id'];
$date = date('Y-m-d H:i:s');
$query = "UPDATE ads SET ad_date = :date WHERE ad_id = :id";
?>
Is there a best way to do that or will a CRON job that is selected by the user going to be too much load on the server to have users setting up CRON jobs continuously.
I saw this one approach and wonder if it is possible to set it up for this purpose. I am still getting familiar with CRON jobs.
How to start/stop a cronjob using PHP?
I will be setting this up on Godaddy's server:
Godaddy Cron jobs
Appreciate the help.
One method is to store the required updates in a new database table, containing fields such as the post id, how many times to update, interval, how many updates are left, the time at which the first update should take place ( or the time for next update ) .
Then, create a php script that fetches data from this table and take necessary action for each post. Check if it's time to update the post, if yes then update, else don't ( check time with accuracy upto minutes, don't check seconds. If you can, do this time checking with SQL and not in php to reduce the data being fetched ). It's better to perform all updates in a single query to maximise efficiency.
Then set up a cron job on the server that runs this php script every minute or whatever minimum time interval you need.
This way, you can get away with just one cron job😉
If you need even more efficiency, then at the end of this script, check if there are any more updates pending, if none then delete the cron job. Then, set up your code such that whenever a user creates a new update, check if the cron job exists, if it doesn't then create it ( this additional dynamic cron job is only for efficiency freaks. If there are frequent update requests, it might be better to avoid creating/deleting cron jobs )

PHP/MySQL update mysql field after 10 minutes has elapsed.

As the title reads, I am looking for a way to update a mysql field after 10 minutes has elapsed of a query being run.
Something like below is an example without the time restraint:
mysql_query("UPDATE `players` SET `playcoins`=TRUNCATE(ROUND((`playcoins`+$amount),9),8) WHERE `id`=$player[id] LIMIT 1");
Any ideas?
MySQL databases have a class of object called an EVENT. It's basically a hunk of SQL code that runs at particular time, or on a particular interval.
You could use code like this to create an event to do what you require at the right time in history. This particular code will create an event that runs just once, ten minutes in the future.
DELIMITER $$
DROP EVENT IF EXISTS coins_user12345$$
CREATE EVENT coins_user12345
ON SCHEDULE
AT NOW() + INTERVAL 10 MINUTE
ON COMPLETION NOT PRESERVE
ENABLE
DO BEGIN
UPDATE players
SET playcoins=TRUNCATE(ROUND((playcoins+123),9),8)
WHERE id=12345
LIMIT 1;
END$$
DELIMITER ;
To use these EVENT objects, you have to configure the event scheduler correctly. Read this. http://dev.mysql.com/doc/refman/5.6/en/events-configuration.html Some cheap shared hosting providers don't allow the use of events, so this is not guaranteed to work.
You go it the wrong way. Sure you can do it. And you can do it with PHP. But you shouldn't. PHP is not the right language to do such a task. Before I starting talk about shell_execute and sleep, which would be the core elements, you need to do this, I offer you another solution.
If I see right, you want to give a player every 10 minutes, some coins.
The right approach would´basicly be:
Save the last time the player has get coins in the database. If you get the player coins, you first want to check, the last time you give the player coins. Now calculate, how much he has earned in this time difference. Finaly add this to his balance and update the field, where you save the last time, the player has earned coins.
An alternative would be a Cronjob/Scheduled Task to a PHP file, which is called every 10 minutes, to give each player the coins, he should get.

Countdown until mysql query is executed

I'm redeveloping my online text based MMO, And I'm building a "Travel" Script Where a user clicks where they want to go and it takes X minutes to get there.
The only problem is - Due to limitations of my host, I cannot run 1 cronjob every minute to take away time spent travelling.
Can someone tell me how i could achieve this without using crons? Delaying a query from being run for multiples of 1 minute or so?
Once they set off, A countdown from that moment of X minutes begins, And once their travel time reaches 0 their "location" is set to "destination"
Thanks!
Use PHP sleep() method.
Like sleep(60); will result in the script to wait execution for 1 minute.
Do NOT set a script timeout as it wont make any sense.
I suggest you add a destination time column, set it to when the animals should arrive, and then in the client display a countdown until that time. You could use a query such as
select * from arrivals where arrival_time < now();
To get a list of animals that have not yet arrived, and of course the arrival_time is when they will arrive.

How can I check and change data in a mysql every 15 sec?

In my web app, user is given 5 min to do some job, and after that 5 min, the job should be passed to other person. So, each job would have user_id and that user_id value has to be changed every 5 min. The thing is that there are multiple jobs, and we want to use 15sec interval for script to be periodically run to take care of this change. And job will passed to other person maximum of 4~5 times, and it will be disregarded after that.
The job & user data is stored in MySQL database.
My initial thought was to use cron-job with PHP script file that let the cron-job runs PHP script every 15 sec. PHP file will read the job table, select all the jobs which have time value past 5 min ago, and fetch them to other users (doesn't matter if one user get the all jobs or not). I just want to periodically run it without taking too much resources.. but is it a good idea? Wouldn't that take too much resources once the number of jobs increases?
While it does not have to be consistence (13~16 sec interval is just fine) we want that script does not stop.
Create a cronjob that reads the table status updated time.
-- show table status like table;
example: update_time: 2011-04-10 15:27:32
than you can do the logic in the cronjob to check that date. and if that date is greater than 15 seconds ago or whatever, than you have an updated records.

Categories