num_rows() not returning correct number of row whe

2019-06-13 08:18发布

问题:

I am working on one Join query in CodeIgniter. Query run perfectly fine. The problem is $query->num_rows().This is how I write my join query.

$this->db->select('related colums');
$this->db->from('table1 as t1');
$this->db->join('table2 as t2', 't1.id = t2.id', 'left outer');
$this->db->join('table3 as t3', 't1.id_second = t3.id', 'left outer');                                                          
$this->db->where('t1.column', $some_varibale);
$this->db->order_by("t1.column", "desc");
$query = $this->db->get();

$query_result = $query->result_array();
$number_of_row = $query->num_rows(); // This line of code is not working properly. 

print_r($number_of_row); 

// To check result is empty or not.

if(!empty($query_result)){
    echo 'not empty';
} else {
    echo "empty";
}

The problem:

When I print the $number_of_row it gives me 13 but when I print $query_result it will show only one row which is correct result.(I have double checked it) so the problem is I was expecting that $query->num_rows() will return me 1 if I get only one row in the result.

I have the cross check it when I get an empty result then it will show 0 as expected. but as described before when I get one row in result it will show 13 number.

I am aware of count and it will work but the question is why this $query->num_rows() not working correctly?

I didn't get that what am I doing wrong?

回答1:

Try this:

$number_of_row = $this->db->affected_rows();

Let me know if that works