当结果不为空时,num_rows() 不返回正确的行数



我正在 CodeIgniter 中处理一个 Join 查询。查询运行良好。 问题是$query->num_rows().这就是我编写连接查询的方式。

$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";
}

问题:

当我打印$number_of_row时,它给了我 13,但当我打印$query_result时,它只会显示一行,这是正确的结果。(我已经仔细检查过了( 所以问题是我期望如果我在结果中只得到一行,$query->num_rows()会返回我 1。

当我得到一个空结果时,我进行了交叉检查,然后它会按预期显示 0。 但如前所述,当我在结果中获得一行时,它将显示 13 个数字。

我知道count并且可以正常工作,但问题是为什么这$query->num_rows()无法正常工作?

我不明白我做错了什么?

试试这个:

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

让我知道这是否有效

最新更新