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

后端 未结 8 1672
南方客
南方客 2020-12-13 17:50

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->cou

8条回答
  •  北海茫月
    2020-12-13 18:28

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

提交回复
热议问题