Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
Warning: mysql_num_rows() expects parameter 1 to be resource,PHP登錄腳本不工作 - 投擲錯誤
我貼在這裏一天一個問題,因爲我的登錄腳本簡單地呈現空白頁,我打開錯誤報告的建議和解決問題,但是現在,這是一個不同的問題,當我登錄時,它有時會將我重定向回登錄屏幕,如果登錄失敗(我檢查了我使用的是正確的密碼),這是重定向的位置。每隔一段時間,儘管它彈出以下錯誤。
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/PropSuite/login.php on line 24
Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/PropSuite/login.php:24) in /Applications/MAMP/htdocs/PropSuite/login.php on line 26
這是我的代碼:
<?php
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
session_start(); //must call session_start before using any $_SESSION variables
$username = $_POST['username'];
$password = $_POST['password'];
//connect to the database here
$hostname_PropSuite = "localhost";
$database_PropSuite = "propsuite";
$username_PropSuite = "root";
$password_PropSuite = "root";
$PropSuite = mysql_pconnect($hostname_PropSuite, $username_PropSuite, $password_PropSuite) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_PropSuite, $PropSuite);
$username = mysql_real_escape_string($username);
$query = "SELECT password, salt
FROM users
WHERE username = '$username';";
$result = mysql_query($query);
if(mysql_num_rows($result) < 1) //no such user exists
{
header('Location: http://localhost/PropSuite/index.php');
die();
}
$userData = mysql_fetch_array($result, MYSQL_ASSOC);
$hash = hash('sha256', $userData['salt'] . Hash('sha256', $password));
if($hash != $userData['password']) //incorrect password
{
header('Location: http://localhost/PropSuite/index.php');
die();
}
else
{
validateUser(); //sets the session data for this user
}
//redirect to another page or display "login success" message
header('Location: your-desired-login-location.php');
die()
//redirect to another page or display "login success" message
?>
我是相當新的PHP所以希望得到您的幫助下,我可能會得到關於直接把數據庫信息的腳本註釋的方式,這將通過連接文件最終完成。
[頭已經通過PHP發送(http://stackoverflow.com/questions/8028957/headers-already-sent-by-php) – deceze
HTTP:/ /stackoverflow.com/a/2973209/476 – deceze
提示:不要使用pconnect。在99%的案件中,這是錯誤的選擇。 – Ron