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

问题描述 投票:3回答:2

我有一个脚本在我的服务器上每分钟运行,基本上做一个cron工作来更新数据库中的一些变量,我正在为一些朋友制作一个小游戏。

我遇到的问题是脚本只运行一些查询,但无法更新其他查询。我已经确认它正在连接,但比较不起作用。任何帮助,将不胜感激。

这是我目前无法正常工作的电话。

//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
php mysql execution
2个回答
2
投票

好吧,让我猜一下,脚本设置为每个人都离线,对吗?问题是你在WHERE声明中错过了UPDATE条款

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参数,我建议使用预备语句。


0
投票

关于您的时间转换的一些注意事项:

$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));
$before = strtotime(date("Y-m-d h:i:s", strtotime($row['dt'])));

在第一行中,将文本"now"转换为时间戳,然后将其转换为字符串,然后再转换为时间戳。一种简短而有效的方法就是将线条拉平

$now = time();

第二行是类似的。替换为:

$before = strtotime($row['dt']);

我一般来说,时间转换比通常的算术慢得多。所以我建议,使用始终unix时间戳(自纪元以来的秒数)并将其转换为仅文本,如果需要在用户界面。

我从不使用数据库类型datetimestamp,但总是与UNIX_TIMESTAMP()使用int。这使得更容易,特别是如果您需要具有不同时区的用户相关输出。你也没有DST的问题。

比较输出:

mysql -e 'select unix_timestamp("2019-03-31 04:00")-unix_timestamp("2019-03-31 02:00")'
mysql -e 'select timediff("2019-03-31 04:00","2019-03-31 02:00")'

结果是

3600
02:00:00 

只有第一个结果是正确的,因为DST开始之间,差异只有1小时。

© www.soinside.com 2019 - 2024. All rights reserved.