我有一個使用MySQL數據庫進行用戶註冊和登錄的Android項目。註冊部分按預期工作。登錄部分沒有。這是事情,他們都使用相同的EXACT查詢來檢查電子郵件地址是否已經存在。註冊檢查以確保用戶在存儲用戶之前尚未使用該電子郵件地址。登錄使用相同的查詢來加密加密密碼+ salt的哈希以與POSTED密碼哈希的哈希進行比較。當登錄部分進行查詢時,它至少返回一行,但看起來所有值都爲空。下面是其中的查詢代碼:查詢似乎返回一個結果,但爲空值
// Check if user already exists.
$sql = sprintf("SELECT * FROM users WHERE email = '%s'", mysql_real_escape_string($email));
$result = mysql_query($sql) or die(mysql_error());
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
// User exists.
$stored_salt = $result["salt"];
$stored_password = $result["encrypted_password"];
$hash = crypt($password_hash, '$2a$10$'.$stored_salt);
// Check for password equality.
if ($stored_password == $hash) {
// User authentication details are correct.
$response["success"] = 1;
$response["uid"] = $result["unique_id"];
$response["user"]["name"] = $result["name"];
$response["user"]["email"] = $result["email"];
$response["user"]["created_at"] = $result["created_at"];
$response["user"]["updated_at"] = $result["updated_at"];
echo json_encode($response);
} else {
// User authentication failed.
// echo JSON with error = 1.
$response["error"] = 2;
$response["error_msg"] = "Incorrect password!";
echo json_encode($response);
}
} else {
// User does not exist.
$response["error"] = 1;
$response["error_msg"] = "Email address not found.";
}
返回的JSON對象說!「密碼不正確」,所以我有它包括,目前以JSON對象覈對密碼,該密碼是空值。我的假設是查詢沒有返回結果,但它通過了no_of_rows> 0測試,所以它返回了一些東西。所以我在返回的JSON對象中包含了結果的其他部分,它們也是空的。我還通過我的網站上的phpmyadmin查詢了查詢(它託管在1and1上),查詢在那裏工作。任何人有任何見解?我對PHP和MySQL都很陌生,因此我在這裏學習,但是我碰到了一堵牆。
'$ result'是 'MySQL的結果資源',你需要用它做什麼,即['mysql_fetch_array'](http://www.php.net/manual/en/function.mysql -fetch-array.php) – Jon 2013-04-08 01:49:50
歡迎來到Stack Overflow! [請不要在新代碼中使用mysql_ *函數](http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到紅色框?學習有關準備語句,並使用[pdo](http://stackoverflow.com/questions/tagged/pdo)或[mysqli](http://stackoverflow.com/questions/tagged/mysqli)。 – Dracs 2013-04-08 02:04:27
@Jon謝謝,這確實有效! – 2013-04-08 02:06:53