简体   繁体   English

CodeIgniter 中的 $query>num_rows() 和 $this->db->count_all_results() 之间的区别 & 推荐哪一个

[英]difference between $query>num_rows() and $this->db->count_all_results() in CodeIgniter & which one is recommended

In a scenario I need to know the count of recordset a query will return, which in codeigniter can be done by $query->num_rows() or $this->db->count_all_results() .在一个场景中,我需要知道查询将返回的记录集计数,在 codeigniter 中可以通过$query->num_rows()$this->db->count_all_results()来完成。 Which one is better and what is the difference between these two?哪个更好,这两者有什么区别?

With num_rows() you first perform the query, and then you can check how many rows you got.使用num_rows()您首先执行查询,然后您可以检查您获得了多少行。 count_all_results() on the other hand only gives you the number of rows your query would produce, but doesn't give you the actual resultset.另一方面, count_all_results()只为您提供查询将产生的行数,但不会为您提供实际的结果集。

// num rows example
$this->db->select('*');
$this->db->where('whatever');
$query = $this->db->get('table');
$num = $query->num_rows();
// here you can do something with $query

// count all example
$this->db->where('whatever');
$num = $this->db->count_all_results('table');
// here you only have $num, no $query

$this->db->count_all_results is part of an Active Record query (preparing the query, to only return the number, not the actual results). $this->db->count_all_resultsActive Record查询的一部分(准备查询,只返回数字,而不是实际结果)。

$query->num_rows() is performed on a resultset object (after returning results from the DB). $query->num_rows()结果集 object上执行(从数据库返回结果后)。

Which one is better and what is the difference between these two Its almost imposibble to me, someone just want to get the number of records without re-touching or perform another query which involved same resource. Which one is better and what is the difference between these two对我来说几乎是不可能的,有人只想获取记录数而无需重新处理或执行涉及相同资源的另一个查询。 Furthermore, the memory used by these two function is in same way after all, since with count_all_result you still performing get (in CI AR terms), so i recomend you using the other one (or use count() instead) which gave you reusability benefits.此外,这两个 function 使用的 memory 毕竟是相同的方式,因为使用count_all_result你仍然执行get (在 CI AR 术语中),所以我建议你使用另一个(或使用 count() 代替)这给了你可重用性好处。

There are two ways to count total number of records that the query will return.有两种方法可以计算查询将返回的记录总数。 First this首先这个

$query = $this->db->query('select blah blah');  
return $query->num_rows();

This will return number of rows the query brought.这将返回查询带来的行数。

Second第二

return $this->db->count_all_results('select blah blah');

Simply count_all_results will require to run the query again.只需 count_all_results 将需要再次运行查询。

We can also use我们也可以使用

return $this->db->count_all('table_name');  

or或者

$this->db->from('table_name');
return $this->db->count_all_result();

or或者

return $this->db->count_all_result('table_name');

or或者

$query = $this->db->query('select * from tab');  
return $query->num_rows();

Total number of results结果总数

$this->db->count_all_results('table name');

Simply as bellow;如下所示;

$this->db->get('table_name')->num_rows();

This will get number of rows/records.这将获得行数/记录数。 however you can use search parameters as well;但是您也可以使用搜索参数;

$this->db->select('col1','col2')->where('col'=>'crieterion')->get('table_name')->num_rows();

However, it should be noted that you will see bad bad errors if applying as below;但是,应该注意的是,如果按照以下方式应用,您将看到严重的错误;

$this->db->get('table_name')->result()->num_rows();
$sql = "select count(*) as row from login WHERE firstname = '" . $username . "' AND password = '" . $password . "'";
    $query = $this->db->query($sql);
    print_r($query);exit;
    if ($query->num_rows() == 1) {
    return true;
    } else {
    return false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM