简体   繁体   中英

PHP mysqli prepared statement for authentication

Im trying to authenticate user using prepared statements. Problem is because stmt always return null . When i use clasic mysqli query all work good.

Check my code:

 public function autheticate($username, $password)
    {

        $query = "SELECT U.username, U.password FROM users as U WHERE U.username = ? AND U.password = ?";

        if($stmt = $this->conn->prepare($this->conn, $query))
        {
            $stmt->bind('ss',$username, $password);

            $stmt->execute();

            $stmt->close();

            return true;
        }

     // print_r($this->conn);
    }

when i do var_dumb($this->autheticate('admin', 'admin')) i get NULL but user exist in database and connection is ok.

When i uncoment print_r($this->conn); i get output:

mysqli Object
(
    [affected_rows] => 
    [client_info] => 
    [client_version] => 50547
    [connect_errno] => 0
    [connect_error] => 
    [errno] => 
    [error] => 
    [error_list] => 
    [field_count] => 
    [host_info] => 
    [info] => 
    [insert_id] => 
    [server_info] => 
    [server_version] => 
    [stat] => 
    [sqlstate] => 
    [protocol_version] => 
    [thread_id] => 
    [warning_count] => 
)

Prepare method for mysqli has different signature than this, what you've used. You should call it in the following way I suppose:

if($stmt = $this->conn->prepare($query))

without passing connection as argument. That's the first bug. Mureinik pointed another issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM