This code work well on first page, but seems that LIMIT don't affect FetchAll(). So my question is: Do fetchAll() have some optional argument that limit number of rows? 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 = "SELECT * FROM test LIMIT $beginning, $perPage";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.