【问题标题】:PHP pagination fetchAll() and LIMITPHP 分页 fetchAll() 和 LIMIT
【发布时间】:2012-11-13 10:44:15
【问题描述】:

此代码在第一页上运行良好,但似乎 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>

【问题讨论】:

    标签: php mysql pagination limit fetchall


    【解决方案1】:

    语法不是LIMIT beginning, end,而是LIMIT beginning, maxrows

    【讨论】:

      【解决方案2】:

      更正这一行:

      $end = $page * $perPage;
      

      与:

      $end = $perPage;
      

      或者把你的sql字符串改成:

      $sql = "SELECT * FROM test LIMIT $beginning, $perPage";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-01-07
        • 2021-05-08
        • 2020-12-30
        • 2015-07-24
        • 2018-08-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多