Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php codeigniter count rows [duplicate]

Tags:

I am new in codeigniter, I want to count all rows from database table but the query dose not retrieve the exact total of rows. Here is Model

public function countRow(){
$query = $this->db->query("SELECT *,count(id) AS num_of_time FROM home");
    // print_r($query->result());
    return $query->result();
}

and this is the controller

public function countTotalrow(){
     $data['query'] = $this->home_model->countRow(); 
}

this is the view

foreach ($num_of_time as $row){
    <span><?php print_r($row->id);?></span>
like image 948
Nilab Avatar asked Mar 10 '15 07:03

Nilab


2 Answers

You could use the helper function $query->num_rows()

It returns the number of rows returned by the query. You can use like this:

$query = $this->db->query('SELECT * FROM my_table');
echo $query->num_rows();
like image 139
marcosh Avatar answered Nov 02 '22 23:11

marcosh


Use this code:

$this->db->where(['id'=>2])->from("table name")->count_all_results();

or

$this->db->from("table name")->count_all_results();

like image 20
Deepak singh Thakur Avatar answered Nov 03 '22 00:11

Deepak singh Thakur