1

In my controller I load my model, then execute the function getAllUserInfo(). Basically that function just does a SELECT in my DB and returns the result() Now back to my controller I want to check the ID of the user that the result() returned to my controller to the ID stored in my session. I'm doing it like so

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

But I'm getting this error

Message: Undefined index: userID

I checked this also on stackoverflow in the following topics, but they didn't really help me

Codeigniter Undefined Index Online Shop

Undefined Index for ... existing index?

4

1 回答 1

0

如果您正在使用,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 选择参考

于 2013-07-22T18:28:26.653 回答