• 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.17/day)
Likes
28
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
#1
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
21,057 (6.22/day)
Likes
10,181
Location
IA, USA
System Name BY-2015
Processor Intel Core i7-6700K (4 x 4.00 GHz) w/ HT and Turbo on
Motherboard MSI Z170A GAMING M7
Cooling Scythe Kotetsu
Memory 2 x Kingston HyperX DDR4-2133 8 GiB
Video Card(s) PowerColor PCS+ 390 8 GiB DVI + HDMI
Storage Crucial MX300 275 GB, Seagate 6 TB 7200 RPM
Display(s) Samsung SyncMaster T240 24" LCD (1920x1200 HDMI) + Samsung SyncMaster 906BW 19" LCD (1440x900 DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay
Audio Device(s) Realtek Onboard, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse SteelSeries Sensei RAW
Keyboard Tesoro Excalibur
Software Windows 10 Pro 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
#2
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
Joined
Nov 13, 2006
Messages
14,414 (3.53/day)
Likes
4,275
System Name VHOST01 | Desktop
Processor i7 980x | i5 7500 Kaby Lake
Motherboard Gigabyte x58 Extreme | AsRock MicroATX Z170M Exteme4
Cooling Prolimatech Megahelams | Stock
Memory 6x4 GB @ 1333 | 2x 8G Gskill Aegis DDR4 2400
Video Card(s) Nvidia GT 210 | Nvidia GTX 970 FTW+
Storage 4x2 TB Enterprise RAID5 |Corsair mForce nvme 250G
Display(s) N/A | Dell 27" 1440p 8bit GSYNC
Case Lian Li ATX Mid Tower | Corsair Carbide 400C
Audio Device(s) NA | On Board
Power Supply SeaSonic 500W Gold | Seasonic SSR-650GD Flagship Prime Series 650W Gold
Mouse N/A | Logitech G900 Chaos Spectrum
Keyboard N/A | Posiden Z RGB Cherry MX Brown
Software Centos 7 | Windows 10
#3
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.17/day)
Likes
28
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
#4
what should i search for on google?

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

Easy Rhino

Linux Advocate
Joined
Nov 13, 2006
Messages
14,414 (3.53/day)
Likes
4,275
System Name VHOST01 | Desktop
Processor i7 980x | i5 7500 Kaby Lake
Motherboard Gigabyte x58 Extreme | AsRock MicroATX Z170M Exteme4
Cooling Prolimatech Megahelams | Stock
Memory 6x4 GB @ 1333 | 2x 8G Gskill Aegis DDR4 2400
Video Card(s) Nvidia GT 210 | Nvidia GTX 970 FTW+
Storage 4x2 TB Enterprise RAID5 |Corsair mForce nvme 250G
Display(s) N/A | Dell 27" 1440p 8bit GSYNC
Case Lian Li ATX Mid Tower | Corsair Carbide 400C
Audio Device(s) NA | On Board
Power Supply SeaSonic 500W Gold | Seasonic SSR-650GD Flagship Prime Series 650W Gold
Mouse N/A | Logitech G900 Chaos Spectrum
Keyboard N/A | Posiden Z RGB Cherry MX Brown
Software Centos 7 | Windows 10
#5
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
10,511 (4.82/day)
Likes
5,596
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#6
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 (1.57/day)
Likes
1,511
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
#7
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
10,511 (4.82/day)
Likes
5,596
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#8
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.