【问题标题】:difference between $query>num_rows() and $this->db->count_all_results() in CodeIgniter & which one is recommendedCodeIgniter 中 $query>num_rows() 和 $this->db->count_all_results() 之间的区别 & 推荐哪一个
【发布时间】:2011-10-25 14:27:33
【问题描述】:

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

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    使用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
    

    【讨论】:

    • 对,我想我会使用count_all_results,我需要在分页中计数,非常感谢:)
    • 如果从优化的角度来看,count_all_results 要好得多。
    【解决方案2】:

    $this->db->count_all_resultsActive Record 查询的一部分(准备查询,只返回数字,而不是实际结果)。

    $query->num_rows()resultset object 上执行(在从数据库返回结果之后)。

    【讨论】:

    • 所以对我来说使用if($this->db->count_all_results()>0{//do stuff} 似乎更好?
    • 如果您只关心找到的结果数量,那么是的
    【解决方案3】:

    我们也可以使用

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

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

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        有两种方法可以计算查询将返回的记录总数。 先这个

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

        这将返回查询带来的行数。

        第二

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

        count_all_results 将需要再次运行查询。

        【讨论】:

          【解决方案6】:

          结果总数

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

          【讨论】:

            【解决方案7】:

            如下所示;

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

            这将获得行数/记录数。但是您也可以使用搜索参数;

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

            但是,应该注意的是,如果按照以下方式应用,您将看到严重的错误;

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

            【讨论】:

              【解决方案8】:
              $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;
                  }
              

              【讨论】:

                猜你喜欢
                • 2013-01-20
                • 2016-07-14
                • 2013-10-23
                • 2012-08-18
                • 1970-01-01
                • 2013-03-24
                • 2017-12-22
                • 2012-03-15
                • 1970-01-01
                相关资源
                最近更新 更多