php 获取错误:“警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,给出的布尔值”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21895021/
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
Getting Error: "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in"
提问by Kelsey
I am getting the errors:
我收到错误:
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/www/thetotempole.ca/phpimageupload/pagecounter.php on line 19
title bodytext
Warning: mysqli_fetch_row() expects parameter 1 to be mysqli_result, boolean given in /home/www/thetotempole.ca/phpimageupload/pagecounter.php on line 32
when I try to run my PHP page. I am expecting this problem is originating from either the $sql or the $connection. I don't believe it is my $connection because all of my variables are correct and I am not getting a connection error. The code is supposed to display my MySQL table's data four rows per page. After four rows have been displayed it will create a new page for the next four rows, and so on.
当我尝试运行我的 PHP 页面时。我预计这个问题源自 $sql 或 $connection。我不相信这是我的 $connection,因为我的所有变量都是正确的,而且我没有收到连接错误。该代码应该每页显示四行我的 MySQL 表的数据。显示四行后,它将为接下来的四行创建一个新页面,依此类推。
Here is my full PHP page's code:
这是我完整的 PHP 页面代码:
<?php
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$dbhost = 'ddm';
$dbuser = 'kdm';
$dbpass = 'Kder';
$dbname = 'kegbm';
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $connection )
{
die('Could not connect: ' . mysqli_error());
}
$start_from = ($page-1) * 4;
$sql = 'SELECT * FROM `testdb` ORDER BY `created` ASC LIMIT "'.$start_from.'",4';
$rs_result = mysqli_query ($connection, $sql);
echo mysqli_error( $connection );
?>
<table>
<tr><td>title</td><td>bodytext</td></tr>
<?php
while ($row = mysqli_fetch_assoc($rs_result)) {
?>
<tr>
<td><? echo $row["title"]; ?></td>
<td><? echo $row["bodytext"]; ?></td>
</tr>
<?php
};
?>
</table>
<?php
$sql = "SELECT COUNT(`created`) FROM `testdb`";
$rs_result = mysqli_query($connection, $sql);
$row = mysqli_fetch_row($rs_result);
$total_records = $row[0];
$total_pages = ceil($total_records / 4);
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='pagination.php?page=".$i."'>".$i."</a> ";
};
?>
采纳答案by DJafari
it's your solution :
这是你的解决方案:
$sql = "SELECT * FROM `testdb` ORDER BY `created` ASC LIMIT $start_from,4";
note:
笔记:
in your code : before and after of $start_from has " that dont must !
在您的代码中:在 $start_from 之前和之后有“那不是必须的!
回答by Justin Wood
The documentationstates
该文件指出
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or
EXPLAIN queries mysqli_query() will return a mysqli_result object. For other
successful queries mysqli_query() will return TRUE.
So your query is most likely failing for some reason. Find out what the raw SQL is, and you will probably be able to spot your error.
因此,您的查询很可能由于某种原因而失败。找出原始 SQL 是什么,您可能会发现您的错误。

