Question

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?

Was it helpful?

Solution

If you are using result() then it returns the object not an array

if there is a single row returned then you can do it like this

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

else you have to loop through the $query to check the user id in the returned result set

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

Here the is CI AR select reference

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top