2017-04-20 178 views
0

你好,我希望將下面的代碼遷移到mysqli。請幫忙,這是登錄功能。第一個函數從數據庫獲取user_id,而第二個函數檢查usernamepassword是否與user_id中的該用戶匹配。mysql查詢到mysqli查詢

function user_id_from_usernam­e($username){ 
    $username = sanitize($username); 
    return mysql_result(mysql_query("SELECT `user_id` FROM `users` WHERE `username` = '$username'"), 0, 'user_id'); 
} 

function login($username, $password){ 
    $user_id = user_id_from_username($username); 
    $username = sanitize($username); 
    $password = md5($password); 
    $query = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'"); 
    return (mysql_result($query­, 0) == 1)? $user_id : false; 
} 
+0

請避免mysql_ *功能,因爲這些都是現在在PHP 7 –

+0

PHP版本棄用請sepcify? –

+1

格式正確請 – nogad

回答

0

爲mysqli_query正確的查詢格式爲:

mysqli_query($connection_variable,"SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '$username' AND `password` = '$password'");