ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

php / mysqli查询未执行某些查询且没有错误

2019-12-11 00:15:42  阅读:174  来源: 互联网

标签:execution mysql php


我的服务器上每分钟都有一个脚本运行,基本上可以完成一项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

解决方法:

好吧,让我猜猜,脚本正在设置为所有人离线,对吗?
问题是您错过了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参数,我还是建议您使用准备好的语句.

标签:execution,mysql,php
来源: https://codeday.me/bug/20191210/2105025.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有