得到错误:“警告:mysqli_fetch_assoc()要求参数1为mysqli_result,布尔值在”

时间:2014-02-19 23:39:15

标签: php mysql mysqli

我收到了错误:

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

当我尝试运行我的PHP页面时。我期待这个问题源于$ sql或$ connection。我不相信这是我的$连接,因为我的所有变量都是正确的,我没有收到连接错误。该代码应该显示我的MySQL表的数据每页四行。显示四行后,它将为接下来的四行创建一个新页面,依此类推。

这是我的完整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> "; 
}; 
?>

感谢您的帮助。感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

这是你的解决方案:

$sql = "SELECT * FROM `testdb` ORDER BY `created` ASC LIMIT $start_from,4";

注意:

你的代码中的

:$ start_from之前和之后有“那不一定!”

答案 1 :(得分:0)

documentation

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.

因此,您的查询很可能由于某种原因而失败。找出原始SQL是什么,你可能会发现你的错误。