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