在我的controller
中加载我的模型,然后执行函数getAllUserInfo()
。基本上该函数只在我的数据库中执行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上也检查了这一点,但它们并没有真正帮助我
答案 0 :(得分: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
}