doumei9589 2016-03-19 22:00
浏览 38

'if'语句条件代码未按预期运行

I genuinely do not understand how this code is messing up but it just is, I've never ran into anything of the sort so if anyone can explain what is going on i'd be very glad.

The code : $functions->update("batzz",$functions->getPlayerInfo("batzz")); runs no matter whether the if condition is met or not, which is starting to get very frustrating.

This is the whole excerpt of code. There is no loop around it, and when it runs, it prints the proper statement but regardless of whether the if is true or false, it runs the update function.

if($functions->checkRecordsExist($_GET['name']) == false) {
    $functions->update("batzz",$functions->getPlayerInfo("batzz"));
    echo "Your account has been updated! Visit your page <a href='/osrstracker/name/". $_GET['name'] ."'>here</a>";
} else {
    echo "Oops, either you havess already updated your account within the past 12 hours or we have messed something up!";
}

I would love to understand how this possibly could happen, and if I can fix it as it is very annoying!

Code for function : $functions->checkRecordsExist($_GET['name']) == false

public function checkRecordsExist($dpname){
        $curdate = date("Y-m-d H:i:s");
        $olddate = date('Y-m-d H:i:s', strtotime('-12 hour'));
        $this->query("SELECT * FROM stats JOIN accounts ON stats.userid = accounts.id  WHERE accounts.displayName=:dpname AND stats.date BETWEEN '$olddate' AND '$curdate';");
        $this->bind(":dpname",$dpname);
        $result = $this->resultSet();

        if(!empty($result)){
            if($result[0]['rank'] == 1){
                $this->query("SELECT * FROM stats JOIN accounts ON stats.userid = accounts.id  WHERE accounts.displayName=:dpname AND stats.date BETWEEN '$olddate' AND '$curdate';");
                $this->bind(":dpname",$dpname);
                $result2 = $this->resultSet();
                if(empty($result2)){
                    return false;
                }

            }
            return true;
        }
        return false;
    }

PDO Object after query being prepared

object(PDOStatement)#3 (1) {
  ["queryString"]=>
  string(171) "SELECT * FROM stats JOIN accounts ON stats.userid = accounts.id  WHERE accounts.displayName=:dpname AND stats.date BETWEEN '2016-03-19 11:48:36' AND '2016-03-19 23:48:36';"
}
  • 写回答

2条回答 默认 最新

  • duanqing3026 2016-03-19 22:57
    关注

    Because you are using == and not === I would wager that you are actually getting a null, empty string or 0 back from that function - something that is equivalent to false but is not a boolean. If you were to use a "=== false" comparison or reverse order and do a "!== false" "or === true" comparison, that will force check the type to boolean and may solve your problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大