警告:mysql_fetch_assoc() 期望参数 1 是给定的资源、对象
我似乎无法弄清楚我做错了什么。因此,当我提交表单时,我收到警告错误并且
注意:未定义变量:/Library/WebServer/Documents/ArturoLuna_Final/loginCheck.php 第 30 行中的 dbusername
$username = $_POST['username'];
$password = $_POST['password'];
if($username&&$password)
{
require 'conn.php';
$query = "SELECT * FROM users WHERE username='$username'";
$result = $mysql->query($query) or die(mysqli_error($mysql));
$numrows = $result->num_rows;
if ($numrows!=0)
{
while($row = mysql_fetch_assoc($result))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//check to see if they match!
if($username==$dbusername&&$password==$dbpassword)
{
echo "youre In!";
}
else
echo "incorrect password!";
}
else
die("that user is dead");
//echo $numrows;
}
else
echo ("Please Enter Username")
我可能做错了什么?
I cant seem to figure out what I'am doing wrong. So when I submit my form I get Warning error and the
Notice: Undefined variable: dbusername in /Library/WebServer/Documents/ArturoLuna_Final/loginCheck.php on line 30
$username = $_POST['username'];
$password = $_POST['password'];
if($username&&$password)
{
require 'conn.php';
$query = "SELECT * FROM users WHERE username='$username'";
$result = $mysql->query($query) or die(mysqli_error($mysql));
$numrows = $result->num_rows;
if ($numrows!=0)
{
while($row = mysql_fetch_assoc($result))
{
$dbusername = $row['username'];
$dbpassword = $row['password'];
}
//check to see if they match!
if($username==$dbusername&&$password==$dbpassword)
{
echo "youre In!";
}
else
echo "incorrect password!";
}
else
die("that user is dead");
//echo $numrows;
}
else
echo ("Please Enter Username")
what can I be possibly doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改
为
您错过了函数名称中的
i
,并且混淆了 OO 和过程式代码,因此您混淆了mysql_*
结果资源和mysqli_result对象。
Change
to
You missed an
i
off the function name, and mixed up OO and procedural style code, so you are mixing upmysql_*
result resources andmysqli_result
objects.您将传统的 PHP MySQL 函数
mysql_*
与 MySQLi 接口混合在一起。在本例中,mysql_fetch_assoc()
需要使用mysql_query()
创建的资源句柄。要检索正确的结果,您需要使用 MySQLi 的方法:
当您使用它时,您可能需要考虑使代码不易受到 MySQL 注入的影响。 正确转义 任何用户提供的输入在将其插入查询字符串之前,或者更好的是,使用 MySQLi 参数绑定工具。
You're mixing traditional PHP MySQL functions
mysql_*
with the MySQLi interface. In this case,mysql_fetch_assoc()
expects a resource handle created withmysql_query()
.To retrieve the correct result, you'll need to use MySQLi's method:
And while you're at it, you might want to consider making the code less susceptible to MySQL injection. Properly escape any user provided input before interpolating it into a query string, or better yet, use MySQLi parameter binding facilities.