rafi
rafi

Reputation: 1543

Count query not working in codeigniter

I have a custom query in codeigniter to count results.

return $this->db->query("SELECT COUNT(*) FROM post WHERE MATCH (title, description)     AGAINST ('+$search_string*' IN BOOLEAN MODE)", NULL, TRUE);

But this is generating only 1 rows, but if i run this query in phpmyadmin then it shows correct results. What is the wrong I am doing?

Upvotes: 0

Views: 9183

Answers (4)

user2164644
user2164644

Reputation: 21

$query =  $this->db->query("SELECT COUNT(*) as totalCounnt FROM post WHERE 
MATCH (title, description) 
AGAINST ('+$search_string*' IN BOOLEAN MODE)", NULL, TRUE);

$result = $query->row();

return $result->totalCount; 

Upvotes: 2

bipen
bipen

Reputation: 36551

i don't think there is extra parameters in query()...docs here

however going thorugh but if i run this query in phpmyadmin then it shows correct results.. you can try pritning the last query that was made just to check if it is correct or not...you can do that by..

 $test=$this->db->query("SELECT COUNT(*) FROM post WHERE MATCH (title, description)     AGAINST ('+$search_string*' IN BOOLEAN MODE)"); //no need of other parameters
 echo $this->db->last_query();exit;

and after query is made you have to generate result...

in yourcase that would be..

  return $this->db->query("SELECT COUNT(*) FROM post WHERE MATCH (title, description)     AGAINST ('+$search_string*' IN BOOLEAN MODE)")->row_array(); 

ways for generating result

Upvotes: 1

Shaolin
Shaolin

Reputation: 2551

This is how count is done using CI active record class

    $this->db->where('name', $name);
    $this->db->where('id !=', $artist_id);
    $this->db->from('artists');

    return $this->db->count_all_results();

Upvotes: 1

Archer
Archer

Reputation: 1162

 $q = $this -> db -> query($your_custom_query);
 return $q -> num_rows();

This should work for you, but you have to replace COUNT(*) by any field in the table or *, i would suggest a field. like say .. title.

You query does not work because $this->db->query resturns and object which needs to be converted to an array, by someting like foreach ($q->result() as $row) {} ..

you may also try return $this -> db -> query($your_custom_query)->result();

Upvotes: 3

Related Questions