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



## Akumos (Aug 7, 2012)

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


----------



## FordGT90Concept (Aug 7, 2012)

You'll have to set up a chrono job with the webhost.  Pretty sure that isn't something PHP/MySQL does itself.


----------



## Easy Rhino (Aug 7, 2012)

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 (Aug 7, 2012)

what should i search for on google?

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


----------



## Easy Rhino (Aug 7, 2012)

Akumos said:


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


----------



## Aquinus (Aug 7, 2012)

Easy Rhino said:


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

```
#!/usr/bin/php
<?php
// Do something here
```

or in bash

```
#!/bin/bash
/usr/bin/php /path/to/your/php/file.php
```

If both cases, make sure you 
	
	



```
chmod +x /path/to/file
```

Here are some crontab examples.

You will want to run this to edit the crontab file.

```
crontab -e
```

Or to view it:

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


----------



## digibucc (Aug 7, 2012)

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 (Aug 7, 2012)

digibucc said:


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


----------

