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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7036950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:51:37  来源:igfitidea点击:

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

phpcodeigniter

提问by Kumar

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(). Which one is better and what is the difference between these two?

在一个场景中,我需要知道查询将返回的记录集计数,这在 codeigniter 中可以通过$query->num_rows()或来完成$this->db->count_all_results()。哪一个更好,这两个有什么区别?

回答by danneth

With num_rows()you first perform the query, and then you can check how many rows you got. 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.

有了num_rows()您先执行查询,然后你可以检查你有多少行了。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

回答by Jens Roland

$this->db->count_all_resultsis part of an Active Recordquery (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()结果集对象上执行(从数据库返回结果后)。

回答by toopay

Which one is better and what is the difference between these twoIts almost imposibble to me, someone just want to get the number of records without re-touching or perform another query which involved same resource. Furthermore, the memory used by these two function is in same way after all, since with count_all_resultyou still performing get(in CI AR terms), so i recomend you using the other one (or use count() instead) which gave you reusability benefits.

Which one is better and what is the difference between these two这对我来说几乎是不可能的,有人只想获得记录数而无需重新触摸或执行涉及相同资源的另一个查询。此外,这两个函数使用的内存毕竟是相同的,因为count_all_result您仍在执行get(在 CI AR 术语中),所以我建议您使用另一个(或使用 count() 代替),这给了您可重用性的好处。

回答by Muhammad Raheel

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 将需要再次运行查询。

回答by Amit Joshi

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();

回答by Deepak singh Thakur

Total number of results

结果总数

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

回答by Shariati

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();

回答by Anandavel A

$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;
    }