php 警告:mysql_fetch_assoc() 期望参数 1 是资源,给定的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8521543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given
提问by Arturo Luna
Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
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
注意:未定义变量:第 30 行 /Library/WebServer/Documents/ArturoLuna_Final/loginCheck.php 中的 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")
what can I be possibly doing wrong?
我可能做错了什么?
回答by DaveRandom
Change
改变
while($row = mysql_fetch_assoc($result))
to
到
while($row = $result->fetch_assoc())
You missed an i
off the function name, and mixed up OO and procedural style code, so you are mixing up mysql_*
result resources and mysqli_result
objects.
你错过了一个i
函数名称,混淆了面向对象和程序风格的代码,所以你混淆了mysql_*
结果资源和mysqli_result
对象。
回答by Linus Kleen
You're mixing traditional PHP MySQL functions mysql_*
with the MySQLi interface. In this case, mysql_fetch_assoc()
expects a resource handle created with mysql_query()
.
您将传统的 PHP MySQL 函数mysql_*
与 MySQLi 接口混合在一起。在这种情况下,mysql_fetch_assoc()
需要使用创建的资源句柄mysql_query()
。
To retrieve the correct result, you'll need to use MySQLi's method:
要检索正确的结果,您需要使用 MySQLi 的方法:
$row = $result->fetch_assoc();
And while you're at it, you might want to consider making the code less susceptible to MySQL injection. Properly escapeany user provided inputbefore interpolating it into a query string, or better yet, use MySQLi parameter binding facilities.
并且在您使用它时,您可能需要考虑使代码不易受到 MySQL 注入的影响。在将任何用户提供的输入插入查询字符串之前,正确地对其进行转义,或者更好的是,使用 MySQLi参数绑定工具。