Active Record Problem |
[eluser]egguzel[/eluser]
Hi everone, Can anyone explain is there any differences between two functions below. The first function works okay but the second one gives an error. Also could you explain what is wrong with second function. Error: A PHP Error was encountered Severity: Notice Message: Undefined property: stdClass::$activated Filename: controllers/user.php Line Number: 224 My Model 1st Function function login_user($username, $password) { $query = $this ->db ->where('username', $username) ->where('password', sha1($password)) ->limit(1) ->get('user'); if ($query->num_rows > 0) { return $query->row(); } else { return false; } } 2nd Function function login_user($username, $password) { $this->db->select('user_id, role'); $this->db->from('user'); $this->db->where('username', $username); $this->db->where('password', sha1($password)); $this->db->limit(1); $query = $this->db->get(); if ($query->num_rows > 0) { return $query->row(); } else { return false; } } My Controller $user_row = $this->user_model->login_user($username, $password); $_SESSION['username'] = $username; $_SESSION['user_id'] = $user_row->user_id; $_SESSION['role'] = $user_row->role;
[eluser]CroNiX[/eluser]
The warning is self-explanitory: Message: Undefined property: stdClass::$activated Filename: controllers/user.php Line Number: 224 You are using a property ($this->activated) that hasn't been defined around line 224 of your user controller. You didn't provide any of that code for us to see from your controller, so the code you posted doesn't really have anything to do with the problem... One thing I do notice is in both functions you are using Code: if ($query->num_rows > 0) { num_rows() is a method, so it should be Code: if ($query->num_rows() > 0) { http://ellislab.com/codeigniter/user-gui...sults.html
[eluser]egguzel[/eluser]
I can ask my question in a different way. Is there any differences between 1st and 2nd code block below? 1st Code Block $query = $this ->db ->where(‘username’, $username) ->where(‘password’, sha1($password)) ->limit(1) ->get(‘user’); 2nd Code Block $this->db->select(‘user_id, role’); $this->db->from(‘user’); $this->db->where(‘username’, $username); $this->db->where(‘password’, sha1($password)); $this->db->limit(1); $query = $this->db->get(); The 2nd code block does not work in the function I had written above (in first post) Thanks
[eluser]boltsabre[/eluser]
Yes.... in the first one you are getting all the details from that row, in the second you are only getting "user_id" and "role". But as has been pointed out the problem is in your controller, not your model. I'd be guessing that it's because of only selecting "user_id" and "role in the second and that you're assuming you have also selected "activated" or something... |
Welcome Guest, Not a member yet? Register Sign In |