Php 是否可以在CodeIgniter中使用where条件来计算所有值?

Php 是否可以在CodeIgniter中使用where条件来计算所有值?,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正试着做这样的事情 $this->db->count_all("grant_money")->where('id',5); echo $this->db->where('id',5)->from("grant_money")->count_all_results(); 可能吗 如果还有其他方法,请告诉我。谢谢 我想用单行代码执行此查询,就像我在上面尝试的那样您可以这样使用它 $this->db->where('id',5); $this

我正试着做这样的事情

$this->db->count_all("grant_money")->where('id',5);
echo $this->db->where('id',5)->from("grant_money")->count_all_results();
可能吗

如果还有其他方法,请告诉我。谢谢


我想用单行代码执行此查询,就像我在上面尝试的那样

您可以这样使用它

$this->db->where('id',5);
$this->db->from("grant_money");
echo $this->db->count_all_results();
这将显示带有where条件的所有计数

用单行线试试这个

$this->db->count_all("grant_money")->where('id',5);
echo $this->db->where('id',5)->from("grant_money")->count_all_results();

尝试使用如下功能获取数据:

$count_all = $this->db->get_data("select count(1) from grant_money where id = 5");
echo $count_all;

您可能需要查看CodeIgniter文档:

文档>数据库参考>数据库生成器类

例如

它接受“查询生成器限制符”,例如
where()
或_-where()
like()
或_-like()
,等等


HTH

是的,您可以尝试这可能是这将是完整的:-

public function record_count_with_where($table_name,$column_name,$type)
  {
    $this->db->select($column_name);
    $this->db->where($column_name,$type);
    $q=$this->db->get($table_name);
    $count=$q->result();
    return count($count);

  }

$this->db->where('id',5)->计算所有结果(“赠款”)

是否可以在CodeiCenter中使用where条件对所有数据进行计数?

基于条件的CI中的计数

$this->db->from('mytable');
$this->db->where('field_name',"comapre_vale");
$this->db->like('filed_name',"any_value");                          
$count = $this->db->count_all_results();

echo $count;
例如:

$this->db->from('tbl_ontime_attendance');
$this->db->where('Card_Number',$value['emp_code']);
$this->db->like('In_Time',"03/2018");

$days = $this->db->count_all_results();
echo $days;

这是一个简单且非常容易的函数,我已经展示了like和where子句的用法,根据您的需要进行更改

尝试
echo$this->db->where('id',5)->计算所有结果('grant_money')在一行和更短的行中`