Baya Baya - 6 months ago 19
SQL Question

Select a value from a table in mysql

I'm trying to learn, so please forgive me for what should be an obvious answer:

Trying to compare the password on the db to an "old password" field, but it wasnt comparing as true, so when I print the password I get from the db I get:

Array ( [0] => stdClass Object ( [password] => 7ed5a78c36a55c07c26469345a00609b ) )


instead of only the value for the password, which would be what I'd like to compare with the user input for the old password.

I have it like this:

function edit_member($user_info, $old_pass)
{
$id_user = $this->session->userdata('id_user');
$user_pass = $this->db->query("SELECT password FROM membros WHERE id_user='" . $id_user . "'")->result();
if (md5($old_pass) == $user_pass) {
$this->db->where('id_user', $id_user);
$this->db->update('membros', $user_info);
redirect('/cpanel');
} else {
//redirect('/edit_account/edit');
print_r("A password não coincide");
echo anchor('/cpanel', 'Voltar');

Answer

It seems that your are using CodeIgniter for your application. The CodeIgniter model returns the value or results in object or array format. So to simplify your code you can update your code with following code.

function edit_member($user_info, $old_pass)
{
    $id_user = $this->session->userdata('id_user');
        $user_pass = $this->db->query("SELECT password FROM membros WHERE id_user='" . $id_user . "'")->row();
      if(isset($user_pass->password) && md5($old_pass) == $user_pass->password){
              $this->db->where('id_user', $id_user);
              $this->db->update('membros', $user_info);
              redirect('/cpanel');
        }else {
             //redirect('/edit_account/edit');
               print_r("A password não coincide");
               echo anchor('/cpanel', 'Voltar');
       }
}