此代码在第一页上运行良好,但似乎 LIMIT 不会影响 FetchAll()。所以我的问题是: fetchAll() 是否有一些限制行数的可选参数?或者建议我解决这个问题的更好方法。
<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>