Dan
Dan

Reputation: 2174

unable to count the result with a condition using codeigniter query

I am trying to count rows of bookdetails table where display_id is as argument. Say i passed $id='3'. But i am not getting the output. I think the code which i am trying is wrong. Please help me to write this query correctly

  //--- Counting the rows of bookdetails of table where display_id is as argument------------------------------------------------------------- 
 public function record_count_for_secondtopBooks($id) {
     $this->load->database(); 
    return $this->db->count_all("bookdetails",array('display_id'=>$id)); 
}

Upvotes: 1

Views: 4654

Answers (6)

tomexsans
tomexsans

Reputation: 4527

$this->db->where('display_id',$id);
$result = $this->db->count_all("bookdetails");

or Chain em'

$result = $this->db->where('display_id',$id)->count_all("bookdetails");

check:

echo'<pre>';
print_r($result);
echo'</pre>';

Upvotes: 0

Mosin
Mosin

Reputation: 608

try this

public function record_count_with_where($table_name,$column_name,$type)
  {
    $this->db->select($column_name);
    $this->db->where($column_name,$type);
    $q=$this->db->get($table_name);
    $count=$q->result();
    return count($count);

  }

Upvotes: 0

Anooj P
Anooj P

Reputation: 346

please try below code

 public function record_count_for_secondtopBooks($id) {

$this->db->where('display_id',$id);
$q = $this->db->get('bookdetails');
return $q->num_rows();
}

Upvotes: 0

Moyed Ansari
Moyed Ansari

Reputation: 8461

count_all returns the number of rows in a particular

echo $this->db->count_all('my_table');

Try this

$this->db->where('display_id', $id);
$this->db->from('bookdetails"');
$this->db->count_all_results();

Upvotes: 4

Edwin Alex
Edwin Alex

Reputation: 5108

Just try this,

$this->db->where('display_id', $id);
$query = $this->db->count_all('bookdetails');

return $query;

Upvotes: 0

Amir Bilal
Amir Bilal

Reputation: 457

count_all accepts only one argument and that is table name. So you will get count of all records in that table. as written in manual:

Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:

Upvotes: 1

Related Questions