我正在尝试使用此代码从数据库创建日期创建24小时的时间段,但是当我想要回显时,我有mysql_fetch_assoc问题,它显示错误的格式我只需要几分钟!
//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";
$res1 = mysql_query($sql1) or die("[1] MySQL ERROR: ".mysql_error());
while ($dif = mysql_fetch_assoc($res1)) {
echo $dif["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());
}
如果您可以访问列的所有日期会更好:)任何人都可以帮忙吗?
=============================================== ========================================= ///////// ///
我把ID放在柜台上。还有其他更好的方法吗?感谢
$idcount = 1;
while ($dif = mysql_fetch_assoc($res1)) {
echo $dif["tdif"];
echo "<br>";
if ($dif["tdif"] >= 86400) { //24h
//update user's page rank
$sql2 = "UPDATE deposit SET `earn` = `earn` + 10 where id=" . $idcount . "";
mysql_query($sql2) or die("[2] MySQL ERROR: ".mysql_error());
答案 0 :(得分:0)
我的猜测这实际上就是你想要做的。请注意,我已将if
语句移至while
循环内,并将$dif
更改为$dif["tdif"]
。
$sql1 = "SELECT TIME_TO_SEC(TIMEDIFF(NOW(), last_ts)) AS tdif FROM php_cron";
$res1 = mysql_query($sql1) or die("[1] MySQL ERROR: ".mysql_error());
while ($dif = mysql_fetch_assoc($res1)) {
echo $dif["tdif"];
if ($dif["tdif"] >= 86400) { //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());
}
}