num_rows() not returning correct number of row when result is not empty

ぐ巨炮叔叔 提交于 2019-12-08 12:15:29

问题


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



来源:https://stackoverflow.com/questions/45397106/num-rows-not-returning-correct-number-of-row-when-result-is-not-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!