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

1

0

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?

Police answered 30/7, 2017 at 6:39 Comment(7)
why don't use count($query_result) ?Therine
thanks for the suggestion count will help me but why this num_row not working is my question. @AidinPolice
move where clause into on clause, see here: #17911229Maleficence
I'm not sure what's the issue with your code but it is indeed correct. Tried replicating it and num_rows() worked as intended. Maybe if you can share more of the actual code, otherwise we can't replicate the problem.Naylor
@Naylor ok i will edit the question and add some of actual code. ThanksPolice
Not sure, why this is not working, because when looking at the source code, it should return the same number as the results returned in result_array(): github.com/bcit-ci/CodeIgniter/blob/3.1-stable/system/database/… Just a guess: Is $query_result an array of arrays or just a one-dimension array. Use print_r($query_result) or debug Codeigniter itself.Arioso
I have tried that both option sir i have used the Codeigniter log and also try to use print_r for result it was same as described in question, for info: this query is the only query in the function of model. @Arioso is join query is doing any thing wrong?Police
D
1

Try this:

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

Let me know if that works

Deuno answered 31/7, 2017 at 7:1 Comment(1)
affected rows behave same as num_rows. I have try that before. thanksPolice

© 2022 - 2024 — McMap. All rights reserved.