douhui3305 2013-07-26 01:36
浏览 38
已采纳

if语句未按预期评估

In the code below, the if statement is evaluated as FALSE where I think it should be TRUE. What am I misunderstanding?

$sql = "SELECT * FROM LIVE";     
$res = mysqli_query($con, $sql);
while ($result = mysqli_fetch_assoc ($res)){
var_dump($result['LIVE']); //string '1' (length=1)
echo $result['LIVE']. "<br>"; //1
}
$a = "1";

if($result['LIVE'] == $a){
echo "It is live!";
}
else {
echo "not live!"; //this gets echoed
}

I have tried all variations of quotes ', ", no quotes

AND changing the data and datatype for the column in the MYSQL table to BIT, CHAR, INT

AND CAST in PHP to BOOL and 'integer'

AND changing the operator eg =, ==, === and have also tried just typing into the if() instead of passing in $a as shown

AND $result = $result['LIVE']; and passing in $result.

I have read PHP type comparison tables and PHP type juggling as well as all the other relative parts of the manual if(), while(), mysqli_fetch_array() etc and can't solve this myself.

I am still learning PHP and I am sure it is an easy fix but I just can't see it.

  • 写回答

4条回答 默认 最新

  • dsfsdf5646 2013-07-26 01:41
    关注

    your not saving the value of $result; during the final iteration of while loop $result will be set to false because there are no more rows to traverse. which is also what triggers to exit the while loop.

    you need to set your value to another temp variable.

    while ($result = mysqli_fetch_assoc ($res)){
    $temp = $result['LIVE']; //1
    }
    $a = "1";
    
    if($temp == $a){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

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