Codeigniter模型的结果是未定义的索引userID


Codeigniter Undefined index userID as result from model

在我的controller中,我加载我的模型,然后执行函数getAllUserInfo()。基本上,该函数只是在我的DB中执行SELECT并返回result()现在回到我的控制器,我想检查result()返回给我的控制器的用户的ID与存储在我的会话中的ID。我这样做

$this->load->model('profile_model');
if($query = $this->profile_model->getAllUserInfo()){
    if($query['userID'] == $this->session->userdata('id')){
        //do something
    }

但我收到这个错误

消息:未定义的索引:userID

我在下面的主题中也在stackoverflow上检查了这一点,但它们并没有真正帮助我

Codeigniter未定义索引网店

的未定义索引。。。现有索引?

如果使用result(),则它返回的对象不是数组

如果有一行返回,那么你可以这样做

if($query = $this->profile_model->getAllUserInfo()){
    if($query[0]->userID == $this->session->userdata('id')){
        //do something
    }
   }

否则,您必须循环使用$query来检查返回结果集中的用户id

if($query = $this->profile_model->getAllUserInfo()){
 foreach($query as $q){
    if($q->userID == $this->session->userdata('id')){
        //do something
    }
    }// end foreach
    }

这里是CI AR选择参考