我尝试使用分页功能每页显示3个项目。 但是我收到了这个错误
Fatal error: Call to a member function bindParam() on boolean in C:\xampp\htdocs\cms\admin2\pagination.php on line 60
我已经搜索了解决方案,他们说主要是导致错误的查询问题,但我的sql查询并不太复杂,使用的语法是正确的,但我不确定为什么错误仍然出现。
这是我的分页代码:
<?php
include "dbConnection.php";
global $dbLink;
try {
// Find out how many items are in the table
$total = $dbLink->query('
SELECT
COUNT(*)
FROM
sponsor_item
')->fetch_row()[0];
// How many items to list per page
$limit = 3;
// How many pages will there be
$pages = ceil($total / $limit);
// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
'options' => array(
'default' => 1,
'min_range' => 1,
),
)));
// Calculate the offset for the query
$offset = ($page - 1) * $limit;
// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);
// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">«</a> <a href="?page=' . ($page - 1) . '" title="Previous page">‹</a>' : '<span class="disabled">«</span> <span class="disabled">‹</span>';
// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">›</a> <a href="?page=' . $pages . '" title="Last page">»</a>' : '<span class="disabled">›</span> <span class="disabled">»</span>';
// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';
// Prepare the paged query
$stmt = $dbLink->prepare('
SELECT
(*)
FROM
sponsor_item
ORDER BY
sponsor_item_id
LIMIT
:limit
OFFSET
:offset
');
// Bind the query params
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();
// Do we have any results?
if ($stmt->rowCount() > 0) {
// Define how we want to fetch the results
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$iterator = new IteratorIterator($stmt);
// Display the results
foreach ($iterator as $row) {
echo '<p>', $row['sponsor_item_id'], '</p>';
}
} else {
echo '<p>No results could be displayed.</p>';
}
} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}
?>
在分页页面上我做错了什么?请指出我的错误。感谢
答案 0 :(得分:-1)
请确保您的查询中有拼写错误,(*)
子句中的SELECT
应为*
。
答案 1 :(得分:-1)
$ stmt是一个布尔值,因为prepare()失败了。所以你需要习惯使用防御性编码实践。
// Prepare the paged query
$stmt = $dbLink->prepare('
SELECT
(*)
FROM
sponsor_item
ORDER BY
sponsor_item_id
LIMIT
:limit
OFFSET
:offset
');
if($stmt === false){
// handle the error here
}