【问题标题】:PHP Activerecord Model CountPHP Activerecord 模型计数
【发布时间】:2019-09-18 22:44:42
【问题描述】:

得到如下结果

$result = MyModel::find_by_id($id);

如何直接计算结果数?

【问题讨论】:

  • 你可以使用count()
  • 怎么样?喜欢这个计数($result)?
  • $count = count($result_array) 这边

标签: php codeigniter codeigniter-2


【解决方案1】:

试试这个,

$this->db->select('*');
$this->db->from('emp');
$query = $this->db->get();
if($query->num_rows()>0){
  return count($query->result());//here you can get count
  //return count($query->result_array());//here you can get count
}

echo $this->db->count_all('emp');

$this->db->like('fname', 'eve');
$this->db->from('emp');
echo $this->db->count_all_results();

【讨论】:

    【解决方案2】:

    尝试简单的解决方案

    $count = $this->db->get('table')->num_rows();
    

    但如果你也想要数据,那么

    $data = $this->db->get('table')->result();
    $count = count($data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多