绑定值时PHP分页获取错误
我正在尝试使用分页功能,每页显示3个项目。 但是我得到了这个错误绑定值时PHP分页获取错误,php,Php,我正在尝试使用分页功能,每页显示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 "dbCon
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>';
}
?>
我在分页页面上做错了什么?请指出我的错误。谢谢非常肯定您的查询中有输入错误,
SELECT
子句中的(*)
应该是*
$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
}
你好,谢谢你的回复。即使我把(*)或*,两者也不能在那里工作,我的查询有问题吗?为什么它返回错误?我已输入正确的表名和订单名。您是否已经检查了pdo/mysqli中提供的错误?我们是来帮助你自己学习的,你知道,所以从帮助自己开始吧。我指出了应该检查错误的位置。阅读maual并检查错误。那你就有你问题的答案了。