1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Website using PHP and MySQL... How do I update a field every hour?

Discussion in 'Programming & Webmastering' started by Akumos, Aug 7, 2012.

  1. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    535 (0.26/day)
    Thanks Received:
    27
    Location:
    Birmingham, England...
    Hi All

    For a pass-time, I am making a text based browser game using PHP... I have a mysql database that the website reads information from. My question is, i have a field in a player table called 'gold'. How do i increase it by 1 every hour?

    Thanks
  2. FordGT90Concept

    FordGT90Concept "I go fast!1!11!1!"

    Joined:
    Oct 13, 2008
    Messages:
    13,319 (6.34/day)
    Thanks Received:
    3,344
    Location:
    IA, USA
    You'll have to set up a chrono job with the webhost. Pretty sure that isn't something PHP/MySQL does itself.
    Akumos says thanks.
    Crunching for Team TPU
  3. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,382 (4.78/day)
    Thanks Received:
    3,218
    ford is correct. that is a server level process. a shell script will include SQL to increase the number by 1 and the cronjob will call that script every hour.
    Akumos says thanks.
  4. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    535 (0.26/day)
    Thanks Received:
    27
    Location:
    Birmingham, England...
    what should i search for on google?

    can someone provide me with a link on how to set this up?
  5. Easy Rhino

    Easy Rhino Linux Advocate

    Joined:
    Nov 13, 2006
    Messages:
    13,382 (4.78/day)
    Thanks Received:
    3,218
    assuming it is a linux box just do 'man crontab' to learn the cron syntax. and assuming you are using the bash shell you will have to create a bash script that opens a connection to your database with the proper credentials and then executes the update command on the field you want.
    Akumos says thanks.
  6. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    5,890 (6.56/day)
    Thanks Received:
    1,888
    Location:
    Concord, NH
    Not true, if you make a .php file executable and you make the first line a definition to the PHP executable like so, you should be able to run PHP in cron, or you can call a php script from a bash script.

    Examples:
    Code:
    #!/usr/bin/php
    <?php
    // Do something here
    or in bash
    Code:
    #!/bin/bash
    /usr/bin/php /path/to/your/php/file.php
    If both cases, make sure you
    Code:
    chmod +x /path/to/file
    Here are some crontab examples.

    You will want to run this to edit the crontab file.
    Code:
    crontab -e
    Or to view it:
    Code:
    crontab -l
    Just remember, what ever user you run this as the crontab will run in. So if you add sudo before all of that it will be root's crontab.

    I do a lot of PHP development on *nix systems for work, so if you have any questions as you work on this please feel free to ask.
    Last edited: Aug 7, 2012
  7. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,863 (2.59/day)
    Thanks Received:
    1,465
    it's possible to do without cron, though it will not be as efficient for the server:

    have your script put an entry in your (mysql) table with a timestamp of the last time 1 gold was added. whenever the script is run check that entry and if it's more than an hour old add one more gold, and update the timestamp.
    Aquinus says thanks.
  8. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    5,890 (6.56/day)
    Thanks Received:
    1,888
    Location:
    Concord, NH
    +1: That is better than using cron. However, using this method and still running a cron job (maybe every 5 minutes since it doesn't take a lot to run,) to do this check and update as needed (a single sql query is all you should need). That way, everything will be up to date at any given point when a user logs in without there being too much of a backlog if no one has logged in for a while. Having it in the script makes sure that it is correct when the user is playing the game. The cron is good to make sure you don't have a backlog of gold updates. Both places could call the same function or method.
    digibucc says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page