【问题标题】:CodeIgniter Active Record - Get number of returned rowsCodeIgniter Active Record - 获取返回的行数
【发布时间】:2010-11-16 09:41:54
【问题描述】:

我对 CodeIgniter 和 Active Record 非常陌生,尤其是我知道如何在普通 SQL 中很好地做到这一点,但我正在努力学习。

如何从我的一个表中选择一些数据,然后使用 CodeIgniters Active Record 类计算返回的行数?

谢谢, 汤姆。

【问题讨论】:

    标签: activerecord codeigniter


    【解决方案1】:

    看看结果函数here:

    $this->db->from('yourtable');
    [... more active record code ...]
    $query = $this->db->get();
    $rowcount = $query->num_rows();
    

    【讨论】:

    【解决方案2】:

    并且,如果您只想获取表中所有行的计数

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

    【讨论】:

      【解决方案3】:

      这给你的模型:

      public function count_news_by_category($cat)
      {
          return $this->db
              ->where('category', $cat)
              ->where('is_enabled', 1)
              ->count_all_results('news');
      }
      

      这是我当前项目中的一个示例。

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

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

      【讨论】:

        【解决方案4】:

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

        echo $this->db
               ->where('active',1)
               ->count_all_results('table_name');
        

        【讨论】:

          【解决方案5】:

          只是要阅读文档儿子!

          $query->num_rows();
          

          【讨论】:

            【解决方案6】:

            您可以通过两种不同的方式做到这一点:

              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.
            

            【讨论】:

              【解决方案7】:
              $this->db->select('count(id) as rows');
              $this->db->from('table_name');
              $this->db->where('active',1);
              $query = $this->db->get();
              foreach($query->result() as $r)
              {
                 return $r->rows;
              }
              

              【讨论】:

              • 这会导致发出SELECT 查询,包括COUNT() SQL 函数调用。应该采用接受的答案,因为它通过调用$query->num_rows() 来防止这种情况。例如,mysqli 驱动程序的num_rows() 返回最后一个查询id 的mysqli_num_rows() PHP 函数的结果。请注意:Not all database drivers have a native way of getting the total number of rows for a result set. When this is the case, all of the data is prefetched and count() is manually called on the resulting array in order to achieve the same result.(来自文档)。
              【解决方案8】:

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

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

              【讨论】:

                【解决方案9】:

                你的模型的这个代码段

                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>
                

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

                【讨论】:

                  【解决方案10】:
                  function getCount(){
                      return $this->db->get('table_name')->num_rows();
                  }
                  

                  【讨论】:

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

                    【讨论】:

                      猜你喜欢
                      • 2014-07-21
                      • 2013-02-22
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-04-07
                      • 1970-01-01
                      • 1970-01-01
                      • 2014-06-26
                      • 1970-01-01
                      相关资源
                      最近更新 更多