php mysqli_fetch_assoc() 期望参数 1 是 mysqli_result,给定布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11347971/
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
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
提问by user1504463
I am pretty new to PHP and MySQL and I just can't figure this one out. I have searched all around the forum but haven't found an answer I can make sense of. I originally was using mysql_fetch_assoc() but I could only search numbers and I received errors when searching for letters as well. I hope I am on the right track here. Thank you in advance for all your help!
我对 PHP 和 MySQL 还是很陌生,我只是想不通。我在论坛上到处搜索,但没有找到我能理解的答案。我最初使用 mysql_fetch_assoc() 但我只能搜索数字并且在搜索字母时也收到错误。我希望我在这里走在正确的轨道上。在此先感谢您的帮助!
$con = mysqli_connect($hostname,$username,$password) or die ("<script language='javascript'>alert('Unable to connect to database')</script>");
mysqli_select_db($con, $dbname);
if (isset($_GET['part'])){
$partid = $_GET['part'];
$sql = 'SELECT *
FROM $usertable
WHERE PartNumber = $partid';
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$partnumber = $partid;
$nsn = $row["NSN"];
$description = $row["Description"];
$quantity = $row["Quantity"];
$condition = $row["Conditio"];
}
回答by Sliq
This happens when your result is not a result (but a "false" instead). You should change this line
当您的结果不是结果(而是“错误”)时,就会发生这种情况。你应该改变这一行
$sql = 'SELECT * FROM $usertable WHERE PartNumber = $partid';
to this:
对此:
$sql = "SELECT * FROM $usertable WHERE PartNumber = $partid";
because the " can interprete $variables while ' cannot.
因为 " 可以解释 $variables 而 ' 不能。
Works fine with integers (numbers), for strings you need to put the $variable in single quotes, like
适用于整数(数字),对于字符串,您需要将 $variable 放在单引号中,例如
$sql = "SELECT * FROM $usertable WHERE PartNumber = '$partid' ";
If you want / have to work with single quotes, then php CAN NOT interprete the variables, you will have to do it like this:
如果你想/必须使用单引号,那么 php 不能解释变量,你必须这样做:
$sql = 'SELECT * FROM '.$usertable.' WHERE string_column = "'.$string.'" AND integer_column = '.$number.';
回答by Arnaud Le Blanc
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
This means that the first parameter you passed is a boolean (true or false).
这意味着您传递的第一个参数是一个布尔值(真或假)。
The first parameter is $result, and it is falsebecause there is a syntax error in the query.
第一个参数是$result,这是false因为查询中存在语法错误。
" ... WHERE PartNumber = $partid';"
You should never directly include a request variable in a SQL query, else the users are able to inject SQL in your queries. (See SQL injection.)
您永远不应该在 SQL 查询中直接包含请求变量,否则用户可以在您的查询中注入 SQL。(请参阅SQL 注入。)
You should escape the variable:
你应该转义变量:
" ... WHERE PartNumber = '" . mysqli_escape_string($conn,$partid) . "';"
Or better, use Prepared Statements.
或者更好的是,使用Prepared Statements.
回答by johnmadrak
You are single quoting your SQL statement which is making the variables text instead of variables.
您正在单引号您的 SQL 语句,它使变量文本而不是变量。
$sql = "SELECT *
FROM $usertable
WHERE PartNumber = $partid";
回答by Ben Ashton
Mysqli makes use of object oriented programming. Try using this approach instead:
Mysqli 使用面向对象的编程。尝试改用这种方法:
function dbCon() {
if($mysqli = new mysqli('$hostname','$username','$password','$databasename')) return $mysqli; else return false;
}
if(!dbCon())
exit("<script language='javascript'>alert('Unable to connect to database')</script>");
else $con=dbCon();
if (isset($_GET['part'])){
$partid = $_GET['part'];
$sql = "SELECT *
FROM $usertable
WHERE PartNumber = $partid";
$result=$con->query($sql_query);
$row = $result->fetch_assoc();
$partnumber = $partid;
$nsn = $row["NSN"];
$description = $row["Description"];
$quantity = $row["Quantity"];
$condition = $row["Conditio"];
}
Let me know if you have any questions, I could not test this code so you might need to tripple check it!
如果您有任何问题,请告诉我,我无法测试此代码,因此您可能需要对其进行三重检查!
回答by TerryE
What happens in your code if $usertableis not a valid table or doesn't include a column PartNumber or part is not a number.
如果$usertable不是有效表或不包含列 PartNumber 或 part 不是数字,代码中会发生什么。
You must escape $partid and also read the document for mysql_fetch_assoc() because it can return a boolean
您必须转义 $partid 并读取 mysql_fetch_assoc() 的文档,因为它可以返回一个布尔值

