Take data from bank and compare CODEIGNITER

Asked

Viewed 892 times

0

Good afternoon, you guys,

I was trying to create a login system for a college job when I came across the following:

I’m using Codeigniter and MYSQL

I have a user database where you have registered the name (primary key) and a password (with encryption).

I was able to insert the data into the table everything ok, but when I try to recover this data from the database to do login validation it gives me an error.

Basically what I want is: Take the value of only one column of my database (the password column) that is encrypted with password_hash and compare with the password passed by the user.

My code where I get my value is like this:

public function loginUsuario($usuario,$senha){
    $this->db->select('*');
    $this->db->from('usuarios');
    $this->db->where('LOGIN',$usuario);
    if($query=$this->db->get()){
        $data = $query->row();
        if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre'){
            return $query->row_array();
        }else{
            return false;
        }
    }

The part 'password' is a test password and with it the login works, I think the error is in the part of:

if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre'){
            return $query->row_array();
        }

Thanks :)

If you need more information leave in the comment.

I appreciate your help.

1 answer

0


To have the result in a row the method is the result and not row

Change that line:

$data = $query->row();

To:

$data = $query->result();

Complete code:

public function loginUsuario($usuario, $senha) {
    $this->db->select('*');
    $this->db->from('usuarios');
    $this->db->where('LOGIN', $usuario);
    if($query = $this->db->get()) {
        $data = $query->result();
        if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre') {
            return (array)$data;
        } else {
            return false;
        }
    }
}

Notice that I also changed row_array() for (array)$data, since you already have the data in an object and then I am transforming it into an array. Another alternative would be to use result_array() in the variable declaration $data and work with array at all (I prefer the latter).

The methods row and row_array exist, but not when working with Query Builder in Codeigniter.

row and row_array are methods of the object that is returned with the method query. Thus:

$query = $this->db->query("YOUR QUERY");

$row = $query->row();
  • Now you can login, but it gives me an error that I’m trying to use a non-object when I get the data from the result_array(), you know what this might be? Thanks for the help ;)

  • I edited the answer to solve this non-object problem. What happens is when we call result() it "wipes" the memory object so you can perform other queries simultaneously.

  • Still the mistake continues: Message: Trying to get property of non-object on the line if (password_verify($senha, $data->SENHA) || $senha == 'senhaMestre') {

  • Are you using result() or result_array()? With result() is an object so to recover the password field will be $data->PASSWORD, with result_array() the return is an array so to recover the password field will be $data['password']

  • Got it here, thanks for the help ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.