0

This is my code and I don't know why IF/ELSE statement is not working

$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);
  }
}
3
  • How is it not working? Commented Oct 19, 2015 at 6:30
  • what error do you get ? Commented Oct 19, 2015 at 6:31
  • in IF clause statement it'is not working in if but else it's working I don't know why Commented Oct 19, 2015 at 6:32

1 Answer 1

2

You can use mysql_num_rows to get count of rows inside results.

$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);
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.