PHP if语句比较总是返回true

时间:2017-09-05 22:51:10

标签: php mysql

我的if语句总是返回true,我无法追逐问题。

$ currentMessageCount的值为42,lastMessageCount为41。

$result = $mysqli->query("SELECT message_count, last_seen
                          FROM message_counter 
                          WHERE match_id = '$i'");
$row = $result->fetch_assoc();
$currentMessageCount = (int)$row["message_count"];
$lastMessageCount = (int)$row["last_seen"];
$result->free();

if ($currentMessageCount > $lastMessageCount) {

    this code is running

} elseif ($currentMessageCount === $lastMessageCount) {

    this code is also running!!

}

编辑:

这是实际的代码示例,按建议添加:

//loop through and check new messages for all matches
for($i = 1; $i <= $numMatches; $i++) {

$result = $mysqli->query("SELECT message_count, last_seen FROM message_counter WHERE match_id = '$i'");
        $row = $result->fetch_assoc();
        $currentMessageCount = (int)$row["message_count"];
        $lastMessageCount = (int)$row["last_seen"];
        //free memory
        $result->free();

        //if new messages - get them, else - skip
        if ($currentMessageCount > $lastMessageCount) {
            $newMessages = true;
            //get NEW messages for that match
            $result = $mysqli->query("  SELECT all_messages.* FROM all_messages
                                        WHERE match_id = '$i' 
                                        AND message_number > '$lastMessageCount'");

            //loop through all messages in table
            $j = 0;
            $allMessages = [];
            while ($row = $result->fetch_assoc()) {
                $dateTime = $row["date_time"];
                $messageNumber = $row["message_number"];
                $messageBody = $row["message_body"];
                $matchID = $i;
                $senderID = $row["sender_id"];

                //message array for each message
                $messageData = ["dateTime" => $dateTime,
                                "messageNumber" => $messageNumber,
                                "currentMessageCount" => $currentMessageCount,
                                "lastMessageCount" => $lastMessageCount,
                                "messageBody" => $messageBody,
                                "matchID" => $matchID,
                                "senderID" => $senderID];

                //put in another array for response
                $allMessages[$j] = $messageData;
                $j++;
            }
            //free memory
            $result->free();

            //put each message set into array for each user
            $allMatchMessages[$i-1] = $allMessages;

        } elseif ($currentMessageCount === $lastMessageCount) {
            $newMessages = false;
        }
    }

代码返回完整数据的$ messageData数组和newMessages = false

1 个答案:

答案 0 :(得分:2)

外部$newMessages循环的每次迭代都会覆盖

for,并最终会得到上一次迭代中分配的值。

  

代码返回的$messageData数组中包含数据,$newMessages = false

对此的明显解释是,$messageData由for循环的早期迭代填充,所有后续迭代(或至少最后一次)将$newMessages设置为false。

看起来您可能只是消除elseif块,并在$newMessages循环之前将false设置为for。实际上,$newMessages可能甚至不是必需的,因为在for循环结束后你可以将$allMessages评估为布尔值。 (如果它是空的,它将评估为假,反之亦然。)