Skip to content
Advertisement

getting error on CodeIgniter Object of class stdClass could not be converted to string in CodeIgniter

I am getting the following error during fetching of database value:

A PHP Error was encountered
Severity: 4096

Message: Object of class stdClass could not be converted to string

Filename: controllers/User.php

Line Number: 184

Backtrace:

File: >Localdrive:xampphtdocsMyRootFolderapplicationcontrollersUser.php Line: 184 Function: _error_handler

File: Localdrive:xampphtdocsMyRootFolderapplicationcontrollersUser.php Line: 151 Function: run

File: Localdrive:xampphtdocsMyRootFolderindex.php Line: 315 Function: require_once

My Controller:

public function changePassword()
 {
    $this->logged_in();

    $data['title'] = "Change Password";

    $this->form_validation->set_rules('oldpass', 'Current Password', 'required|callback_password_check');
    $this->form_validation->set_rules('newpass', 'New Password', 'required');
    $this->form_validation->set_rules('passconf', 'Confirm Password', 'required|matches[newpass]');

    $this->form_validation->set_error_delimiters('<div class="error">','</div>');

    if($this->form_validation->run() == false){

        $user= $this->session->userdata('user');
        $data['user'] = $user;

    $this->load->view('header', $data);
    $this->load->view('change_password', $data);
    $this->load->view('footer', $data);
 }
 else{

        $email = $this->session->userdata('email');
        $newpass = $this->input->post('newpass');
        //$this->User_model->update_user($id, $sessArray)

         
   $this->User_model->update_user($id, array('password' => password_hash($newpass), PASSWORD_BCRYPT));

        

        redirect('User/logout');

 }

}

 public function password_check($oldpass)
{

    //print($oldpass);
    //exit();
    $email = $this->session->userdata('email');
 $user = $this->User_model->get_user($email);
 print($user);
        exit();

    //print($oldpass);
    //exit();

    

    if(password_verify($oldpass, $user['password']) == true){


    $this->form_validation->set_message('password_check', 'The {field} does not match');

    return false;
    }

    return true;
}

My Model:

function getData(){
    $query = $this->db->where('register');
    return $query;
}

public function get_user($email)
{
    $this->db->where('email', $email);
    $query = $this->db->get('register');
    return $query->row();
}

public function update_user($email, $sessArray)
{

    //$this->db->set($data);
    $this->db->where('email', $email);
    $this->db->update('register', $sessArray);
}

My View:

<div class="row justify-content-center" style="margin-top: 50px;">
<div class="col-4">
    <h1><?php echo $title ?></h1>
    <?php echo form_open('User/changePassword', array('id' => 'passwordForm'))?>
        <div class="form-group">
            <input type="password" name="oldpass" id="oldpass" class="form-control" placeholder="Current Password" />
            <?php echo form_error('oldpass', '<div class="error">', '</div>')?>
        </div>
        <div class="form-group">
            <input type="password" name="newpass" id="newpass" class="form-control" placeholder="New Password" />
            <?php echo form_error('newpass', '<div class="error">', '</div>')?>
        </div>
        <div class="form-group">
            <input type="password" name="passconf" id="passconf" class="form-control" placeholder="Confirm Password" />
            <?php echo form_error('passconf', '<div class="error">', '</div>')?>
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-success">Change Password</button>
        </div>
    <?php echo form_close(); ?>
</div>

Advertisement

Answer

You are requesting the data as an object in your model but trying to us it as an array. Either change this line: if(password_verify($oldpass, $user->password) == true){ to use the fetch as object or change get_user func to return $query->row_array(); to use it as an array. P.s.: for debug use var_dump both for array and object instead of print

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement