doushansu9012 2016-12-13 21:49
浏览 27

PHP检查行和会话是否为空

im trying to check if the row in table is empty and the session also in , can i know what im doing wrong here.

 if (empty($row['images']) && (!isset($_SESSION['picture']) || $_SESSION['picture'] == ''))

EDIT : posting the code as im trying to post some divisions based on the picture, and if there are no picture i post a demo. i hope i explained it right.

if (mysqli_multi_query($conn, $sql)) {
    do {
        if ($result = mysqli_store_result($conn)) {
            while ($row = mysqli_fetch_array($result)){
                if (empty($row['images']) && (!isset($_SESSION['picture']) || $_SESSION['picture'] == '')){
                    echo "<div>" ;
                    echo "                  <img src='images/logo.png' alt=""/>";
                    echo "                  <h1>This Is A Demo</h1>";
                    echo "                  <p>This is a Demo paragraph .</p>";
                    echo"                   <a href='../go_back.php'>Demo</a>";
                    echo "</div>" ;
                } else{
                    echo "<div>" ;
                    if(!empty($row['images'])) {
                        echo "                  <img src='images/users_iamge.png' alt=""/>";
                        echo "                  <h1>Not Demo</h1>";
                        echo "                  <p>This is Not Demo paragraph .</p>";
                    }else{ // fetch the session
                        echo "                  <img src='images/users_iamge.png' alt=""/>";
                        echo "                  <h1>Not Demo</h1>";
                        echo "                  <p>This is Not Demo paragraph .</p>";
                    }
                }       
                echo'<h1>' 
                echo'<p>'
                echo "</div>";                  
            }
            mysqli_free_result($result);
        }   
    } while (mysqli_more_result($conn));
}
  • 写回答

1条回答 默认 最新

  • duanbin198788 2016-12-13 22:24
    关注

    Your logic appears to be wrong. Recapping basic operators in php.

    The second condition in an or is only evaluated if the first condition evaluates to false.

    Hence if $_SESSION['picture'] is set, the evaluation of $_SESSION['picture'] == '' will never occur (as the first condition of the or evaluates to true).

    To get the functionality you desire (truetable below)

    Empty  | SEmpty  | SNull  | Result
    -------+---------+--------+---------
      0        0         0        0
      0        0         1        0
      0        1         0        0
      0        1         0        0
      1        0         0        0
      1        0         1        1
      1        1         0        1
      1        1         1        1
    

    You need:

    if( empty($row['images']) && 
           ( (isset($_SESSION['picture']) && $_SESSION['picture'] == '') ||
             !isset($_SESSION['picture']) ) 
      )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错