2013-02-28 233 views
2

我試圖開發一個學校項目的登錄系​​統,但我遇到了錯誤,我不知道如何解決它。從數據庫獲取沒有數據

在我的數據庫中,我有密碼spy123存儲在sha256哈希,但是當我試圖抓住它在PHP中,我沒有得到任何回報。

當前代碼:

// checks if the user/password combination works. 
// $username | String - the username of the user to login 
// $password | 32byteHash - the hashed password of the user 
// return | Boolean - Returns true if the password works for that user. 
function forumLib_checkLogin($username, $password){ 
    $goodLogin = false; //Assume the password is wrong. 

    $obtainUserPassword_query = "SELECT password FROM forum_users WHERE username = '$username'"; 

    $dbc = forumLib_connectToDB(); 
    $results = mysqli_query($dbc, $obtainUserPassword_query); 
    forumLib_closeConnectionToDB($dbc); 

    echo "query: $obtainUserPassword_query <br />"; 
    echo "results: ".mysqli_fetch_array($results)."<br />"; 
    $row = mysqli_fetch_array($results); 
    echo "row[0]: ".$row[0]."<br />"; 
    echo "password: $password <br />"; 
    echo "DBpassword: $passwordOnDB <br />"; 

    //if user exists 
    if($results != false){ 
     //get password from DB 
     $row = mysqli_fetch_array($results); 
     $passwordOnDB = $row['password']; 

     //check password 
     if($password == $passwordOnDB){ 
      //successfull login 
      $goodLogin = false; 
     } 
    } 

    return $goodLogin; 
} 

輸出:

change password request detected 
query: SELECT password FROM forum_users WHERE username = 'SpyMaster356' 
results: Array 
row[0]: 
password: 7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24 
DBpassword: 
Current password does not match records. 

SQL查詢是沒有錯的,因爲我得到7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24回來時,我手動輸入。

這真讓我困惑。

+0

而不是echo使用print_r($ row)來查看真正從數據庫中提取的內容。您還應該測試mysqli_query()調用的返回值以查看它是否失敗 – Cfreak 2013-02-28 17:58:06

+0

每執行一次'mysqli_fetch_array',結果集中的光標都會遞增。 – hjpotter92 2013-02-28 17:58:19

+0

另外,輸出'7d9519224964c72a90b342c565fab1e4582d0e974043798f940b471304db0e24'不是由查詢生成的;它是由提供給函數的'$ password'生成的。 – hjpotter92 2013-02-28 18:02:51

回答

4

一旦你第一次打電話給mysqli_fetch_array你已經消耗了第一行數據。隨後,當你嘗試把它捕捉到$row變量,該行已經被消耗掉,而不是保存:

// Prints 'Array'. Did not save the result 
echo "results: ".mysqli_fetch_array($results)."<br />"; 

// Doesn't get anything because the row has been previously fetched 
$row = mysqli_fetch_array($results); 
+0

啊,讓我們看看是否會修復它然後 – 2013-02-28 18:00:39

+0

它確實。我發現真正的問題是什麼。 '//檢查密碼 if($ password == $ passwordOnDB){ //成功登錄 $ goodLogin = false; // <--- NO。它應該是真的。 DOPE }' – 2013-02-28 18:09:19

0

更改follwing segement

echo "query: $obtainUserPassword_query <br />"; 
echo "results: ".mysqli_fetch_array($results)."<br />"; 
$row = mysqli_fetch_array($results); 
echo "row[0]: ".$row[0]."<br />"; 
echo "password: $password <br />"; 
echo "DBpassword: $passwordOnDB <br />"; 

先填充$row陣列,並將其用於進一步echo

echo "query: $obtainUserPassword_query <br />"; 
$row = mysqli_fetch_array($results); 
echo "results: ". print_r($row) ."<br />"; 
echo "row[0]: ".$row[0]."<br />"; 
echo "password: $password <br />"; 
echo "DBpassword: {$row[0]} <br />";