【问题标题】:num_rows() not returning correct number of row when result is not empty当结果不为空时,num_rows() 未返回正确的行数
【发布时间】:2018-01-05 22:00:48
【问题描述】:

我正在 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() 不能正常工作?

我不明白我做错了什么?

【问题讨论】:

  • 为什么不用count($query_result)
  • 感谢建议计数会帮助我,但为什么这个 num_row 不起作用是我的问题。 @艾丁
  • 将 where 子句移到 on 子句中,参见此处:stackoverflow.com/questions/17910729/…
  • 我不确定您的代码有什么问题,但它确实是正确的。尝试复制它,num_rows() 按预期工作。也许如果你能分享更多的实际代码,否则我们无法复制问题。
  • @hyubs 好的,我将编辑问题并添加一些实际代码。谢谢

标签: php codeigniter codeigniter-3 mysql-num-rows


【解决方案1】:

试试这个:

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

让我知道这是否有效

【讨论】:

  • 受影响行的行为与 num_rows 相同。我以前试过。谢谢
猜你喜欢
  • 1970-01-01
  • 2016-04-08
  • 1970-01-01
  • 2011-01-15
  • 1970-01-01
  • 2021-02-28
  • 2015-03-31
  • 1970-01-01
  • 2019-01-22
相关资源
最近更新 更多