<?php
//connect to database
//....
//prepare MySQL query
$sql = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
//execute query and handle errors
//...
//optinal reporting on e-mail
//...
//end
?>
0 0 * * * /usr/bin/php -f /path/to/my_update_script.php > /dev/null
CREATE TABLE `php_cron` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`last_ts` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `php_cron` (`id`, `last_ts`) VALUES (1,'2012-08-10 00:00:00');
<?php
//connect to database
//....
//get time difference in seconds from last execution
$sql1 = "SELECT TIME_TO_SEC(TIMEDIFF(NOW(), last_ts)) AS tdif FROM php_cron WHERE id=1";
$res1 = mysql_query($sql1) or die("[1] MySQL ERROR: ".mysql_error());
$dif = mysql_fetch_assoc($res1['tdif']);
if ($dif >= 86400) { //24h
//following code will run once every 24h
//update user's page rank
$sql2 = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
mysql_query($sql2) or die("[2] MySQL ERROR: ".mysql_error());
//update last execution time
$sql3 = "UPDATE php_cron SET last_ts = NOW() WHERE id=1";
mysql_query($sql3) or die("[3] MySQL ERROR: ".mysql_error());
}
?>
crontab -e
0 0 * * * php /var/www/myquery.php