• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

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

Joined
Nov 30, 2008
Messages
555 (0.09/day)
Location
Birmingham, England...
Processor Intel Core 2 Quad Q6600 @ 2.8GHz
Motherboard Gigabyte X48T-DQ6
Cooling Zalman 9500 LED CPU Cooler
Memory 2x 2GB Corsair DDR3 XMS3 DHX - 1600MH/PC3-12800
Video Card(s) Gigabyte HD4870 1GB
Storage 2x Seagate 320GB Barracuda (RAID 0) 3x 1TB Samsung F3, 140GB WD Maxtor (10,000rpm)
Display(s) 2x 20" LG Flatron L204WS
Power Supply Powercool 850W
Software Windows 7 Ultimate x64
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
 
You'll have to set up a chrono job with the webhost. Pretty sure that isn't something PHP/MySQL does itself.
 
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.
 
what should i search for on google?

can someone provide me with a link on how to set this up?
 
what should i search for on google?

can someone provide me with a link on how to set this up?

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.
 
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.

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:
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.
 
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.

+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.
 
Back
Top