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?

Community
  • 1
  • 1
mXX
  • 3,595
  • 12
  • 44
  • 61

1 Answers1

0

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

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • Yep this did the trick. You say it doesn't return an array, but you still look it up like an array with `$query[0]->userID` – mXX Jul 22 '13 at 18:35
  • but it has the objects in it not the whole array like `$query[0]['query']` this is the whole array but it `$query[0]->userID` is the object with the index – M Khalid Junaid Jul 22 '13 at 18:39