错误:获取非对象的属性 [英] Error : get property of non-object

查看:203
本文介绍了错误:获取非对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

调用非对象上的成员函数


我收到这个错误:尝试获取非对象的属性[从模型]
当我尝试从数据库中检索id通过输入一个字符串,在这种情况下是'something' / p>

这是我的模型

  function getRoleId($ role_name = )
{

$ condition = array('role_name'=> $ role_name);
$ this-> db-> where($ conditions);
$ this-> db-> select('id');
$ query = $ this-> db-> get('roles');
$ row = $ query-> row()
return $ row-> id;
}



控制器($ radioRole这里是= something),但是当它到达模型成为别的东西。

  $ radioRole ='something'; 
$ insertData ['role_id'] = $ this-> user_model-> getRoleId($ radioRole);

UPDATE

  print_r($ radioRole); 
$ insertData ['role_id'] = $ this-> user_model-> getRoleId($ radioRole);

显示$ radioRole,在这种情况下为something



但是当它到达user_model和当我做一个print_r它显示别的东西,'买家',它甚至不显示print_r做在控制器。感觉像是写的:O但我不知道:O



print_r($ query)

  CI_DB_mysql_result对象([conn_id] =>资源ID#37 [result_id] =>资源ID#64 [result_array] => Array()[result_object] =& [current_row] => 0 [num_rows] => 0 [row_data] =>)


解决方案

您的其中错了:

 code> $ conditions = array('role_name'=> $ role_name); 
$ this-> db-> where($ conditions);

应为

  $ conditions = array('role_name',$ role_name); 
$ this-> db-> where($ conditions);





<



,如果条件直接在中,则必须使用逗号,因为 / p>

  $ this-> db->其中('role_name',$ role_name) 


Possible Duplicate:
Call to a member function on a non-object

I receive this error : Trying to get property of non-object [from Model] When i try to retrieve id from database by inputting a string which in this case is 'something'

This is my Model

   function getRoleId($role_name='')
         {

            $conditions = array('role_name'=> $role_name);
            $this->db->where($conditions);
            $this->db->select('id');
            $query = $this->db->get('roles');       
            $row   = $query->row();
            return $row->id;
         }

Controller ($radioRole here is = something) but when it reaches the model it becomes something else.

$radioRole  = 'something';
$insertData['role_id']            = $this->user_model->getRoleId($radioRole);

UPDATE

print_r($radioRole);
$insertData['role_id']            = $this->user_model->getRoleId($radioRole);

Shows $radioRole which is in this case 'something'

but when it reaches user_model and when i do a print_r it shows something else, 'buyer' , it doesnt even show the print_r done on controller. Feels like its being over written :O but i got no idea :O

print_r($query)

  CI_DB_mysql_result Object ( [conn_id] => Resource id #37 [result_id] => Resource id #64 [result_array] => Array ( ) [result_object] => Array ( ) [current_row] => 0 [num_rows] => 0 [row_data] => )

解决方案

Your where is wrong:

$conditions = array('role_name'=> $role_name);
$this->db->where($conditions);

should be

$conditions = array('role_name', $role_name);
$this->db->where($conditions);

EDIT:

as mentioned in the comments, the comma is necessary if the condition is directly within the where as such:

$this->db->where('role_name',$role_name);

这篇关于错误:获取非对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆