簡體   English   中英

PHP為什么If / Else語句在While循環中不起作用?

[英]PHP Why If / Else statement not working in While loop?

這是我的代碼,我不知道為什么IF / ELSE語句不起作用

$user_id = $_POST['user_id'];

$strSQL = "SELECT chat.user_id,max(date) max_date ,user.user_id,user.firstname,user.lastname,user.picture 
    FROM ( select from_user_id as user_id,date,isread from chat 
      WHERE to_user_id = '$user_id' 
      Union select to_user_id as user_id,date,isread from chat WHERE from_user_id = '$user_id' ) as chat join user on user.user_id = chat.user_id 
    where user.status != 'block' group by chat.user_id order by max_date DESC";
      $chatdata = array();
      $objQuery = mysql_query($strSQL);

         while($row = mysql_fetch_assoc($objQuery)){
          $frduser_id = $row['user_id'];
          $firstname = $row['firstname'];
          $lastname = $row['lastname'];
          $picture = $row['picture'];
          $strSQL2 = "SELECT isread from chat WHERE to_user_id = '$user_id' and from_user_id = '$frduser_id'
                      ORDER BY date DESC LIMIT 1";

           //array_push($chatdata, $row);
      $objQuery2 = mysql_query($strSQL2);
      if (empty($objQuery2)) {
          $row2 = 1;
          $result = array_merge($row, $row2);
          array_push($chatdata,$result);
      }else{
      $row2 = mysql_fetch_assoc($objQuery2);
       $result = array_merge($row, $row2);
      array_push($chatdata,$result);
  }
}

您可以使用mysql_num_rows來獲取結果中的行數。

$objQuery2 = mysql_query($strSQL2);
$objQuery2Rows = mysql_num_rows($strSQL2);

if ( $objQuery2Rows < 1) {
      $row2 = 1;
      $result = array_merge($row, $row2);
      array_push($chatdata,$result);    
 }else{
      $row2 = mysql_fetch_assoc($objQuery2);
      $result = array_merge($row, $row2);
      array_push($chatdata,$result);
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM