简体   繁体   中英

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

I have model query but i am getting error as Message: count(): Parameter must be an array or an object that implements Countable in some of the systems and in some of the systems its working fine.Here is the query were i am getting the issues.

     public function getChatPlanProvidersInfoById($planId) {
    $providersArr = array();
    $this->db->select('ppm.price as plan_price, ppm.offer_start, ppm.offer_end, ppm.validity_period, pipm.provider_id, pipm.provider_group_id');
    $this->db->from('chatinstance_provider_mapping pipm');
    $this->db->join('chatplan_instance ppm', 'ppm.id = pipm.chatplan_instance_id');
    $this->db->where('pipm.chatplan_instance_id', $planId);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        $providersInfo = $query->result();
        if (count($providersInfo) > 0) {
            $providersArr['offerInfo'] = array('offer_start' => $providersInfo[0]->offer_start, 'offer_end' => $providersInfo[0]->offer_end, 'valid_period' => $providersInfo[0]->validity_period);
            $providersArr['plan_price'] = $providersInfo[0]->plan_price;
            foreach ($providersInfo as $provider) {
                if (!empty($provider->provider_id)) {
                    $pInfo = $this->getProviderInfoById($provider->provider_id);
                    $providersArr['providerInfo'][] = $pInfo;
                } else if (!empty($provider->provider_group_id)) {
                    $pInfo = $this->getProviderGroupInfoById($provider->provider_group_id);
                    if (count($pInfo) > 0) {
                        $providersArr['providerInfo'][] = $pInfo;
                    } else {
                        $providersArr['providerInfo'][] = array();
                    }
                }
            }
        }
    }
    return $providersArr;
}

Getting error in this if condition count($pInfo)

                   if (count($pInfo) > 0) {
                        $providersArr['providerInfo'][] = $pInfo;
                    } else {
                        $providersArr['providerInfo'][] = array();
                    }

The reason it work on your system is that may be you are using PHP version 5.6 or < 7.2

As on Version 7.2 you will get warning if you pass null or false to count()

$data[5]  = 7;
var_dump(count($data)); // It will return 1


var_dump(count(null)); // It will give Warning: count(): Parameter must be an array or an object that implements Countable in...

var_dump(count(false)); // It will give Warning: count(): Parameter must be an array or an object that implements Countable in...

So in your case

You should check $pInfo before pass to count() as below

if ($pInfo && count($pInfo) > 0) {...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM