phpcodeignitermysql-num-rows

Returned value from CodeIgniter's query()->num_rows is never greater than zero


I cannot seem to get the below query to run and return num_rows correctly. No matter what happens, $query->num_rows > 0 always returns false even when I expect it to return true. Any ideas?

$post_id = $this->input->post('post_id');
$poster_id = $this->input->post('poster_id');
$my_id = $this->session->userdata('id');

$query = $this->db->query("SELECT * FROM default_post_likes
                           WHERE liker_id = '$my_id'
                           AND post_id = '$post_id'");

if ($query->num_rows > 0) {
    echo json_encode(array('error' => 'e1'));
} else {
    $data = array(
        'poster_id' => $poster_id,
        'post_id' => $post_id,
        'liker_id' => $my_id
    );
    
    $this->db->insert('default_post_likes', $data);

    echo json_encode(array('success' => true));
}

Solution

  • $query->num_rows should be a function $query->num_rows()