I recently experienced a very strange error. I am writing a password reset function bu开发者_Python百科t i am experiencing a very strange error i have never experienced before. Somehow, form validation turns my query into empty array.
Here is my code:
public function password_reset()
{
if ($this->auth_model->is_logged_in()) redirect ('welcome');
$userid = $this->uri->segment(3);
$code = $this->uri->segment(4);
$this->db->select('forgot_password, id');
$this->db->from('users');
$this->db->limit(1);
$this->db->where('id',$userid);
$q = $this->db->get();
if (!is_numeric($this->uri->segment(4, 0)) == TRUE && !is_numeric($this->uri->segment(3, 0)) == TRUE) die;
if ($q->row('forgot_password') == $code) {
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
$this->form_validation->set_rules('confirm_password', 'Confirm', 'required|min_length[4]');
if ($this->form_validation->run() !== false) {
if ($this->input->post('password') != $this->input->post('confirm_password')) exit("wrong");
$update = array (
'password' => sha1($this->input->post('password'))
);
$id = $q->row('id');
$this->db->where('id', $id);
$this->db->update('users', $update);
echo $this->db->last_query();
die; // Don't mind the last 2 lines, they were made for debugging purposes.
echo "Password is reset";
} $this->load->view('auth/password_reset');
}
}
Please note this line: $id = $q->row('id');
This is where the problem begins.
If i echo this line, will get "Array", i tried doing print_r on it but it appears to be completely empty array.
However, if i try echoing
$q->row('id');
before
if ($this->form_validation->run() !== false) {
then, i get what is expected, in my case it will be integer, but again, once form validation will be passed, it will turn my integer into empty array.
So here we are, form validation somehow turns my query row into an array.
Any help will be much appreciated.
Where you are using this:
$q = $this->db->get();
//...
if ($q->row('forgot_password') == $code) {
You should fetch the object using row()
only once, and then reference the properties of that object, rather than call row()
over and over.
So, to get you on track to fixing this (there may be other issues with your code but we won't know until getting past this issue), try something like this:
$row = $this->db->get()->row();
That fetches the result (the first row). Done, now you have the result stored in an object and you don't need to fetch it again.
Now, when you want to reference a property of that object, the value of the column in this case, you would use this:
if ($row->forgot_password == $code) {}
//
$id = $row->id;
And so on. Have another read through the docs for more demos: http://codeigniter.com/user_guide/database/results.html
Better to use result_array()
and check the first records against the parameter...
$q = $this->db->select('forgot_password, id')
->from('users')
->limit(1)
->where('id',$userid)
->get();
$res = $q->result_array();
$forgot_password = (count($res[0]) == 1) $res[0]['forgot_password'] : FALSE;
...
// And change this line
// if ($q->row('forgot_password') == $code) {
// into
if ( ! $forgot_password)
{
// Theres no record match with $userid
}
elseif($forgot_password == $code)
{
// Everythings fine...
}
else
{
// Theres a record match with $userid, but the $code not match
}
精彩评论