CodeIgniter Forums
Help on count function error - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Help on count function error (/showthread.php?tid=70891)



Help on count function error - mimingko - 06-14-2018

Hi,

I am encountering the error:

Message: count(): Parameter must be an array or an object that implements Countable

I am using it as:

if (count($query)>0)

{


...

}

Need help on how to resolve the issue


RE: Help on count function error - hipm - 06-14-2018

// 1.create the data object
$data = new stdClass();
$data->query = $query;

// 2.create the array data
$data = [];
$data['query'] = 'SELECT ...';


RE: Help on count function error - InsiteFX - 06-14-2018

Why not use this?

PHP Code:
    // -----------------------------------------------------------------------

    /**
     * get()
     *
     * @param ┬ástring $orderBy
     * @return object
     */
    
public function get($orderBy '')
    {
        
$data = [];

        
$query $this->db->order_by($orderBy)->get($this->tableName);

        if (
$query->num_rows() > 0)
        {
            
$data $query->result();
        }

        
$query->free_result();
        
        return 
$data;
    }