CodeIgniter Active Record - Get number of returned rows

前端 未结 11 1988
不思量自难忘°
不思量自难忘° 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 22:59

    You can do this in two different ways:

      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.
    
    0 讨论(0)
  • 2020-12-12 22:59

    This code segment for your Model

    function getCount($tblName){
        $query = $this->db->get($tblName);
        $rowCount = $query->num_rows();
        return $rowCount;       
    }
    

    This is for controlr

      public function index() {
        $data['employeeCount']= $this->CMS_model->getCount("employee");
        $this->load->view("hrdept/main",$data);
    }
    

    This is for view

    <div class="count">
      <?php echo $employeeCount; ?>                                                                                            
     </div>
    

    This code is used in my project and is working properly.

    result

    0 讨论(0)
  • 2020-12-12 23:00
    function getCount(){
        return $this->db->get('table_name')->num_rows();
    }
    
    0 讨论(0)
  • 2020-12-12 23:01

    AND, if you just want to get a count of all the rows in a table

    $table_row_count = $this->db->count_all('table_name');
    
    0 讨论(0)
  • 2020-12-12 23:01
    $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;
    
    0 讨论(0)
  • 2020-12-12 23:04

    Have a look at the result functions here:

    $this->db->from('yourtable');
    [... more active record code ...]
    $query = $this->db->get();
    $rowcount = $query->num_rows();
    
    0 讨论(0)
提交回复
热议问题