[英]PHP pagination fetchAll() and LIMIT
This code work well on first page, but seems that LIMIT don't affect FetchAll(). 这段代码在首页上运作良好,但似乎LIMIT不会影响FetchAll()。 So my question is: Do fetchAll() have some optional argument that limit number of rows?
所以我的问题是:fetchAll()是否有一些可选的参数来限制行数? Or suggest my a better way to solve this problem.
或建议我解决该问题的更好方法。
<html>
<body>
<?php
require_once('connection.php');
$rowsCount = $connection->query('SELECT COUNT(*) FROM test')->fetchColumn();
$page = 1;
$perPage = 10;
if ( isset( $_GET["page"] ) and $_GET["page"] >= 1 and $_GET["page"] <= 10 ) {
$page = (int) $_GET["page"];
}
$beginning = ($page-1) * $perPage;
$end = $page * $perPage;
$sql = "SELECT * FROM test LIMIT $beginning, $end";
?>
<table border = '2'>
<tr>
<th width="30%">ID</th>
<th width="70%">TEXT</th>
</tr>
<?php
// echo '<pre>';
// print_r($connection->query($sql)->fetch());
// echo $sql;
// echo '<pre>';
foreach ($connection->query($sql) as $value) {
echo '<tr height ="50px"><td>' . $value['id'] . '</td><td>' . $value['text'] . '</td></tr>';
}
?>
</table>
<p>
<?php if($page > 1){ ?>
<a href="pagination.php?page=<?php echo $page-1;?>" >Previous</a>
<?php } ?>
<a href="pagination.php?page=<?php if($page < ceil($rowsCount/$perPage)){ echo $page+1;} else {echo 1;}?>" >Next</a>
</p>
</body>
</html>
语法不是LIMIT beginning, end
,而是LIMIT beginning, maxrows
。
correct this line: 更正这一行:
$end = $page * $perPage;
with: 与:
$end = $perPage;
Or change your sql string to: 或将您的sql字符串更改为:
$sql = "SELECT * FROM test LIMIT $beginning, $perPage";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.