CodeIgniter Active Record - Get number of returned rows

前端 未结 11 1989
不思量自难忘°
不思量自难忘° 2020-12-12 22:48

I\'m very new to CodeIgniter and Active Record in particular, I know how to do this well in normal SQL but I\'m trying to learn.

How can I select some data from one

相关标签:
11条回答
  • 2020-12-12 23:04

    This is also a very useful function if you are looking for a rows or data with where condition affected

    function num_rows($table)
        {   
           return $this->db->affected_rows($table);
        }
    
    0 讨论(0)
  • 2020-12-12 23:08

    If you only need the number of rows in a query and don't need the actual row data, use count_all_results

    echo $this->db
           ->where('active',1)
           ->count_all_results('table_name');
    
    0 讨论(0)
  • 2020-12-12 23:09

    Just gotta read the docs son!

    $query->num_rows();
    
    0 讨论(0)
  • 2020-12-12 23:12
    $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;
    }
    
    0 讨论(0)
  • 2020-12-12 23:20

    This goes to you model:

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

    It'a an example from my current project.

    According to benchmarking this query works faster than if you do the following:

    $this->db->select('*')->from('news')->where(...); 
    $q = $this->db->get(); 
    return $q->num_rows();
    
    0 讨论(0)
提交回复
热议问题