1

I am trying to check if result returned by database is zero but it always falls to zero following is my code

if(isset($_POST['cust_id'])){
    $cust_id = $_POST['cust_id'];
    $stmt = $connect->prepare("SELECT DISTINCT send_stamp, message, time, status FROM `chat` WHERE cust_id=?");
    $stmt->bind_param('i', $cust_id);

}
else if(isset($_POST['receiverid'])) {
    $receiverid = $_POST['receiverid'];
    $stmt = $connect->prepare("SELECT DISTINCT send_stamp, message, time, status FROM `chat` WHERE receiverid=?");
    $stmt->bind_param('i', $receiverid);

}

if($stmt->execute()){
    $data  = array();
    if($stmt->num_rows ==0){
        $data[] = array(
           'message'=> 'No messages found',
           'status'=>0,
        );
    }
    else{
        $result = $stmt->get_result();
        while ($row =mysqli_fetch_array($result)) { 
            $data[] = array(
                'message'=> $row['message'],
                'time'=> $row['time'],
                'status'=> $row['status'],
            );
        }   
    }
}
echo json_encode( $data); 

Please help me to figure out this situation . Thanks

11
  • I am not sure if this worked, havent tested it but can you var dump your num_rows Commented Dec 7, 2017 at 10:51
  • you can trace with echo exit which condition is execute Commented Dec 7, 2017 at 10:52
  • int(0) [{"message":"No messages found","status":0}]null problem is that i do have records against particular id Commented Dec 7, 2017 at 10:53
  • Have you checked that the query returns rows if you run it in phpMyAdmin or something like that Commented Dec 7, 2017 at 10:55
  • Which query is the problem? One, or both? Presumably the query doesn't return any results. Have you checked the correct query is running (and therefore that the correct POST values were sent)? Also, a basic question (but I've seen questions on here where OP had not actually checked this!) do the expected rows actually exist in the database you're connecting to? Are you connecting to the right instance of the database? Commented Dec 7, 2017 at 10:55

1 Answer 1

3

try with this one

execute query and get data into result like $result=$stmt->execute() and that result compare in if() condition like if($result) and inside if($result == 0) so it give an result

$result=$stmt->execute();
if($result)
{  
    $data  = array();
    if($result ==0){
        $data[] = array(
               'message'=> 'No messages found',
               'status'=>0,
            );
    }
    else{

    $result = $stmt->get_result();
        while ($row =mysqli_fetch_array($result)) { 
            $data[] = array(
               'message'=> $row['message'],
               'time'=> $row['time'],
               'status'=> $row['status'],
            );
        }   
    }
}

OR

also write $result = $stmt->get_result(); after if($stmt->execute()) and compare if($result->num_rows ==0) like this way

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.