Error : get property of non-object [duplicate]

2020-04-16 03:51发布

问题:

This question already has answers here:
Closed 7 years ago.

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] => )

回答1:

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);