gpt4 book ai didi

php/mysqli 查询没有执行一些没有错误的查询

转载 作者:可可西里 更新时间:2023-11-01 08:23:31 24 4
gpt4 key购买 nike

我有一个脚本每分钟在我的服务器上运行一次,基本上是一个 cron 作业,为我为一些 friend 制作的小游戏更新数据库中的一些变量。

我遇到的问题是该脚本仅运行某些查询但无法更新其他查询。我已验证它正在连接,但比较无效。任何帮助将不胜感激。

这是我当前无法正常通话的电话。

//Query server
$query = "SELECT id, usr, dt, is_online FROM player_data WHERE is_online =1";
$result = mysqli_query($conn, $query);
// If connection is good
if ($result = mysqli_query($conn, $query))
{
echo "Connected </br>";
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result))
{
//Get time of NOW, and time of last activity
echo "loop started </br>";
$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));
$before = strtotime(date("Y-m-d h:i:s", strtotime($row['dt'])));
//Add five minutes to the last activity
$then = $before + (60 * 5);
//Display the times
echo $before . "</br>";
echo $now . "</br>";
echo $then . "</br>";
//determine if the time now is more then 5 minutes after the last activity
if (strtotime($now) > strtotime($then))
{
//set user to offline if more then 5 minutes has passed.
mysqli_query($conn, "UPDATE player_data SET is_online = 0");
echo "1.User: " . $row['usr'] . "</br>";
echo "1.Database: " . $row['dt'] . "</br>";
echo "1.System: " . date('Y-m-d H:i:s') . "</br>";
echo "Now: " . $now . "</br>";
echo "Before: " . $before . "</br>";
echo "then: " . $then . "</br>";
}

}

}
`````````````````````````````````````````````````````````````````````````
this should set anyone who has not been active in the last 5 minutes to is_online = 0 but does not.

This is the actual output as of right now with the last activity being more then 1.5 hours earlier.


Connected
loop started
1555687200
1555777824
1555687500
loop started
1555687227
1555777824
1555687527

最佳答案

好吧,让我猜猜,脚本是将所有人设置为离线,对吧?问题是您在我们的 UPDATE 语句中错过了 WHERE 子句

mysqli_query($conn, "UPDATE player_data SET is_online = 0");

所以最好复习那部分。应该是这样的

mysqli_query($conn, "UPDATE player_data SET is_online = 0 WHERE id = " . $row['usr']);

也没有必要比较 strtotime($now)strtotime($then):$now $then 已经保存了“microtime”值,所以你可以这样做:

if ($now > $then) {
//your code here
}

顺便说一句,即使您没有使用任何REQUEST 参数,我也建议您使用准备好的语句。

关于php/mysqli 查询没有执行一些没有错误的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55770284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com