0

Alright, so im fairly new to MySQL and databases in general. And i want to make an update on the database after a set amount of time. Let me explain.

So for practise i'm building a game in php, and in this game you will be able to upgrade stuff. Say a building, upgrade it from level 1 to level 2 will take 2 hours. How can i make this event/scheluded on the database so that if the user loggs out it will still update even if he is offline? It's a multiplayer game so i kinda want the update to go through even if the player is offline.

I have tried to google and read up, but i cant realy get a clear understanding of how i would design and code it. I guess i will need an "events" table that will store all current upgrades that are qued and join each of them with a players unique ID? But then, how can i make it update on a set interval so all qued events take place when they are done?

If someone would be kind enought to give me an example or a walkthrough, i would greatly appreciate your time and effort. Thanks in advance!

Naxor
  • 47
  • 9
  • http://stackoverflow.com/questions/18737407/how-to-create-cron-job-using-php – linktoahref Feb 06 '16 at 13:51
  • 1
    imo, I would: keep a record, for the active user, of the next time it is to be updated. Then, as already answered, the `cron job` does the required processing when the time has expired. Run the `cron job` every few minutes to determine which users need processing. It scales, as you can run multiple jobs, in parallel. – Ryan Vincent Feb 06 '16 at 13:56

2 Answers2

1

ONe way is to write a script that polls the database and updates the apropriate rows. Schedule the script to run let's say every minute and use a timestamp to find what rows you should update each time you run the script.

If you are using linux, setup cron to run the script. You could also call the script from a web browser using AJAX.

-1

I think that one easy way could be execute a php script using cron table on your webserver. You can schedule your script to run every 2 hours and it will update desired rows in mysql.

dantella
  • 66
  • 4