Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord CodeIgniter活动记录-获取返回的行数_Activerecord_Codeigniter - Fatal编程技术网

Activerecord CodeIgniter活动记录-获取返回的行数

Activerecord CodeIgniter活动记录-获取返回的行数,activerecord,codeigniter,Activerecord,Codeigniter,我对CodeIgniter和Active Record非常陌生,尤其是,我知道如何在普通SQL中做好这项工作,但我正在努力学习 如何从一个表中选择一些数据,然后使用CodeIgniters活动记录类计算返回的行数 谢谢, Tom.查看结果函数: 如果您只想获得表中所有行的计数 $table_row_count = $this->db->count_all('table_name'); 儿子,我得去看医生 $query->num_rows(); 如果只需要查询中的行数,而不需

我对CodeIgniter和Active Record非常陌生,尤其是,我知道如何在普通SQL中做好这项工作,但我正在努力学习

如何从一个表中选择一些数据,然后使用CodeIgniters活动记录类计算返回的行数

谢谢,
Tom.

查看结果函数:


如果您只想获得表中所有行的计数

$table_row_count = $this->db->count_all('table_name');

儿子,我得去看医生

$query->num_rows();

如果只需要查询中的行数,而不需要实际的行数据,请使用


这适用于您的模型:

public function count_news_by_category($cat)
{
    return $this->db
        ->where('category', $cat)
        ->where('is_enabled', 1)
        ->count_all_results('news');
}
这是我当前项目的一个例子

根据此查询,执行以下操作比执行以下操作更快:

$this->db->select('*')->from('news')->where(...); 
$q = $this->db->get(); 
return $q->num_rows();

如果要查找条件受影响的行或数据,这也是一个非常有用的函数

function num_rows($table)
    {   
       return $this->db->affected_rows($table);
    }

您可以通过两种不同的方式执行此操作:

  1. $this->db->query(); //execute the query
     $query = $this->db->get() // get query result
     $count = $query->num_rows() //get current query record.

  2.  $this->db->query(); //execute the query
      $query = $this->db->get() // get query result
      $count = count($query->results()) 
          or   count($query->row_array())  //get current query record.

此代码段适用于您的模型

function getCount($tblName){
    $query = $this->db->get($tblName);
    $rowCount = $query->num_rows();
    return $rowCount;       
}
这是给controlr的

  public function index() {
    $data['employeeCount']= $this->CMS_model->getCount("employee");
    $this->load->view("hrdept/main",$data);
}
这是供观赏的

<div class="count">
  <?php echo $employeeCount; ?>                                                                                            
 </div>

此代码已在我的项目中使用,并且工作正常

$sql = "SELECT count(id) as value FROM your_table WHERE your_field = ?";
$your_count = $this->db->query($sql, array($your_field))->row(0)->value;
echo $your_count;

我的头撞了好几个小时,我想不起函数的名称。。。在官方文件中也不容易找到。我在对象上使用了
print\r
,该对象由
get\u where
方法生成,用于猜测函数名!=)@Residuum您能帮我解决这个问题吗?这会导致发出一个
SELECT
查询,包括
COUNT()
SQL函数调用。应该采用接受的答案,因为它通过调用
$query->num_rows()
来防止这种情况。例如,mysqli驱动程序的
num_rows()
返回最后一个查询id的
mysqli_num_rows()
PHP函数的结果。请注意:
并非所有数据库驱动程序都有获取结果集总行数的本机方法。在这种情况下,将预取所有数据,并手动调用结果数组上的count(),以获得相同的结果。
(来自文档)。CI3:
function getCount($tblName){
    $query = $this->db->get($tblName);
    $rowCount = $query->num_rows();
    return $rowCount;       
}
  public function index() {
    $data['employeeCount']= $this->CMS_model->getCount("employee");
    $this->load->view("hrdept/main",$data);
}
<div class="count">
  <?php echo $employeeCount; ?>                                                                                            
 </div>
$sql = "SELECT count(id) as value FROM your_table WHERE your_field = ?";
$your_count = $this->db->query($sql, array($your_field))->row(0)->value;
echo $your_count;