php hash function checking database

2019-09-08 16:32发布

问题:

When inserting a new user into a database i want to hash the password. this is what i have got.

    static function getLastId(){
        global $database; 
        $sql = 'SELECT ID from users ORDER BY id DESC LIMIT 1' ;
        $result = $database->query($sql);           
        return $result; 
    }


static function create_user($username,$password ){
    global $database;
    $lastID =  self::getLastId() + 1;       
    $ePassword = $database->escape_value($password);
    $hash = hash('sha256', $lastID . $ePassword);
    $sql = "INSERT INTO ". self::$table_name . " (username, password, first_name, last_name, power ) VALUES ";
    $sql .= "('{$database->escape_value($username)}', '{$hash}', 'asd' , 'asd', 'user') ";
    $query = $database->query($sql);
    return ($query)? true : false; 
}

when i do a print or var dump of $lastID I get 13. however looking a the database i only have 1 user me, with an ID of 1. I even truncated the table, but i am still getting 13. not sure why.

basically the idea is that i want to append $lastID to the password, so that it is much harder to reverse. I need to know what the next ID is going to be, for login purposes.

回答1:

$result = $database->query($sql);           
return $result; 

You're returning the result resource instead of actual data (i assume so, unless your $database module automatically does this), you might want to:

$result = $database->query($sql);    
$data = mysql_fetch_array($result);
return $data['ID']; 


标签: php sql oop