This question already has an answer here:
这个问题在这里已有答案:
- mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource 31 answers
- mysql_fetch_array()/ mysql_fetch_assoc()/ mysql_fetch_row()/ mysql_num_rows等...期望参数1为资源31答案
I have a function that takes an SQL query and parameters/array of parameters. This function working fine on UPDATE
statements but not on SELECT
statements.The function mysqli_fetch_array
is not returning anything and I cannot figure out why. The error message I am getting is;
我有一个函数,它接受SQL查询和参数/参数数组。这个函数在UPDATE语句上工作正常但在SELECT语句上没有。函数mysqli_fetch_array没有返回任何内容,我无法弄清楚原因。我得到的错误信息是;
"mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given"
“mysqli_fetch_array()期望参数1为mysqli_result,boolean给出”
I am using PHP version 5.5.
我使用的是PHP 5.5版。
The query is as follows:
查询如下:
<?php
$txtSearch = isset($_POST["txtSearch"]) ? $_POST["txtSearch"] : "";
$result_search = null;
$query = "";
if(!empty($txtSearch))
{
if($page == "adverts")
{
$query = "
SELECT
A.ID,
A.title,
A.productname,
A.price,
A.description,
A.productlocation,
(SELECT I.name FROM image I WHERE I.advertID = A.ID LIMIT 1) AS name
FROM advert A
WHERE A.title LIKE ?";
}
$result_search = queryDB($query, array("s", "%".$txtSearch."%"));
}
while($row = mysqli_fetch_array($result_search))
{
echo
"<div class='post_container'>
<div class='title_banner'>".$row["title"]."</div>
<div>£".$row["price"]."</div>
</div>";
}
?>
2 个解决方案
#1
0
You could return $stmt->get_result()
instead, which will provide you with a result you can iterate over.
您可以返回$ stmt-> get_result(),它将为您提供可以迭代的结果。
Your error tells you that you are returning a boolean, not a mysqli_result. The documentation on mysqli_stmt::fetch()
confirms that it returns a boolean.
您的错误告诉您正在返回布尔值,而不是mysqli_result。 mysqli_stmt :: fetch()上的文档确认它返回一个布尔值。
#2
0
Try to check query result before you're passing it as an argument. If it returns false, than there is something wrong with the query.
在将查询结果作为参数传递之前,请尝试检查查询结果。如果返回false,则查询出错。
if($result_search === FALSE) {
die(mysql_error()); // TODO: better error handling
}
And I would change your query on something like this:
我会改变你的查询,如下所示:
SELECT
A.ID, A.title, A.productname,
A.price, A.description,
A.productlocation,
I.name
FROM
advert A
INNER JOIN
image I ON I.advertID = A.ID
WHERE
A.title LIKE ?"
#1
0
You could return $stmt->get_result()
instead, which will provide you with a result you can iterate over.
您可以返回$ stmt-> get_result(),它将为您提供可以迭代的结果。
Your error tells you that you are returning a boolean, not a mysqli_result. The documentation on mysqli_stmt::fetch()
confirms that it returns a boolean.
您的错误告诉您正在返回布尔值,而不是mysqli_result。 mysqli_stmt :: fetch()上的文档确认它返回一个布尔值。
#2
0
Try to check query result before you're passing it as an argument. If it returns false, than there is something wrong with the query.
在将查询结果作为参数传递之前,请尝试检查查询结果。如果返回false,则查询出错。
if($result_search === FALSE) {
die(mysql_error()); // TODO: better error handling
}
And I would change your query on something like this:
我会改变你的查询,如下所示:
SELECT
A.ID, A.title, A.productname,
A.price, A.description,
A.productlocation,
I.name
FROM
advert A
INNER JOIN
image I ON I.advertID = A.ID
WHERE
A.title LIKE ?"