• 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?

Joined
Nov 30, 2008
Messages
555 (0.10/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
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
You'll have to set up a chrono job with the webhost. Pretty sure that isn't something PHP/MySQL does itself.
 

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,447 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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.
 
Joined
Nov 30, 2008
Messages
555 (0.10/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
what should i search for on google?

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

Easy Rhino

Linux Advocate
Staff member
Joined
Nov 13, 2006
Messages
15,447 (2.42/day)
Location
Mid-Atlantic
System Name Desktop
Processor i5 13600KF
Motherboard AsRock B760M Steel Legend Wifi
Cooling Noctua NH-U9S
Memory 4x 16 Gb Gskill S5 DDR5 @6000
Video Card(s) Gigabyte Gaming OC 6750 XT 12GB
Storage WD_BLACK 4TB SN850x
Display(s) Gigabye M32U
Case Corsair Carbide 400C
Audio Device(s) On Board
Power Supply EVGA Supernova 650 P2
Mouse MX Master 3s
Keyboard Logitech G915 Wireless Clicky
Software The Matrix
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
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:
Joined
May 21, 2009
Messages
4,966 (0.91/day)
System Name i7-PC / HTPC / iMac
Processor i7 3820 / Phenom II 940
Motherboard GIGABYTE G1.ASSASSIN2 / M3A79-T Deluxe
Cooling Corsair Hydro H100i / Scythe II (HS only)
Memory G.SKILL Trident X Series 8GB (2 x 4GB) DDR3 1600mhz / 4GB DDR2 1066 (@800) Corsair Dominator
Video Card(s) GB Radeon HD 7950s 3GB / GB Radeon HD 7950s 3GB
Storage 2x 80GB Intel X-25, 2x600gb SATA, 1x1tb 5400RPM storage /1x600GB, 3x500GB,1x160,1x120 SATA
Display(s) 1x 27" Yamakasi / Vizio 42" HDTV
Case Lian Li Lancool PC-K58 / Antec 900
Audio Device(s) HT Omega Striker 7.1 / Onboard and HDMI from ATi Card
Power Supply PC Power & Cooling 750W / 610W
Software Ubuntu / Windows 8.1 Pro / OS X / PHPStorm / Gaming
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

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
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.
 
Top