'Codeigniter 3 - Call to a member function num_rows() on bool

I have this Codeigniter3 query.

$this->db->select("COUNT(city) AS totals");
$this->db->from('contacts');
$this->db->group_by('totals'); 
$this->db->order_by('totals', 'DESC'); 
            
$query =  $this->db->get();

if($query->num_rows() > 0) {
            
    foreach($query->result() as $row) {
        $data[] = $row;
    }
    return $data;
}        

After moving the application to a server with mysql 5.7+ I get this error:

Message: Call to a member function num_rows() on bool

I know that can solve adding ONLY_FULL_GROUP_BY.

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

But is there a way to solve it by code?



Solution 1:[1]

You can do this. Please try below method,

 $this->db->select('city_id, COUNT(city_id) as total');
 $this->db->group_by('city_id'); 
 $this->db->order_by('total', 'desc'); 
 $this->db->get('tablename', 10);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pream Dev