我有这个错误 mysql_fetch_assoc()当我尝试登录系统时,参数1是资源,第121行给出的布尔。这是我的特定问题的代码部分。有谁知道为什么这会发生在代码中?
if(isset($_POST["loginbtn"]))
{
$uid = $_POST["userid"];
$pword = $_POST["userpass"];
$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
");
if($row = mysql_fetch_assoc($result))
{
$_SESSION ["loggedin"] = "true" ;
$_SESSION ["adid"] = $row ["Ad_ID"];
$_SESSION ["adname"] = $row ["Ad_Name"];
$_SESSION ["adaright"] = $row ["Ad_ARight"];
if($row ["Ad_ARight"]=='write')
header('Location: HomeScreen.php');
else if($row ["Ad_ARight"]=='read')
header('Location: HomeScreenr.php');
}
else
{
echo '<div class="alert-message error"><p><b> Invalid username or
password </b></p></div>';
}
}
?>
<div align="center">
答案 0 :(得分:1)
您的查询失败,因此mysql_query
返回了布尔值false。
找出错误的最佳方法是添加错误处理程序。
$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'") or die(mysql_error());
答案 1 :(得分:0)
使用正确的Ad_ID和Ad_Pass执行查询,然后查看是否显示任何结果。可能结果为空。
SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
答案 2 :(得分:0)
答案 3 :(得分:0)
尝试使用 mysql_num_rows 进行检查,否则请使用mysql_error()检查您的查询。
if(isset($_POST["loginbtn"]))
{
$uid = $_POST["userid"];
$pword = $_POST["userpass"];
$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'
") die(mysql_error());
$num=mysql_num_rows($result);
if($num)
{
$row = mysql_fetch_assoc($result)
$_SESSION ["loggedin"] = "true" ;
$_SESSION ["adid"] = $row ["Ad_ID"];
$_SESSION ["adname"] = $row ["Ad_Name"];
$_SESSION ["adaright"] = $row ["Ad_ARight"];
if($row ["Ad_ARight"]=='write')
header('Location: HomeScreen.php');
else if($row ["Ad_ARight"]=='read')
header('Location: HomeScreenr.php');
}
else
{
echo '<div class="alert-message error"><p><b> Invalid username or
password </b></p></div>';
}
}
?>
请检查此代码。我不确定它是否正常工作。 我认为它会完美运作。
答案 4 :(得分:-1)
尝试像这样检查。检查是否设置了$result
并返回一个值。然后将数组分配给$row
:
if(isset($result))
{
$row = mysql_fetch_assoc($result)
$_SESSION ["loggedin"] = "true" ;
$_SESSION ["adid"] = $row ["Ad_ID"];
$_SESSION ["adname"] = $row ["Ad_Name"];
$_SESSION ["adaright"] = $row ["Ad_ARight"];
if($row ["Ad_ARight"]=='write')
header('Location: HomeScreen.php');
else if($row ["Ad_ARight"]=='read')
header('Location: HomeScreenr.php');
}
您还可以尝试检查它是否返回任何行:
$result = mysql_query("SELECT * FROM admin WHERE Ad_ID = '$uid' AND Ad_Pass = '$pword'");
$num_rows = mysql_num_rows($result);
if($num_rows > 0)
{
$row = mysql_fetch_assoc($result)
//CODE HERE
}