<small id='3dYWY'></small><noframes id='3dYWY'>

      <bdo id='3dYWY'></bdo><ul id='3dYWY'></ul>

    <i id='3dYWY'><tr id='3dYWY'><dt id='3dYWY'><q id='3dYWY'><span id='3dYWY'><b id='3dYWY'><form id='3dYWY'><ins id='3dYWY'></ins><ul id='3dYWY'></ul><sub id='3dYWY'></sub></form><legend id='3dYWY'></legend><bdo id='3dYWY'><pre id='3dYWY'><center id='3dYWY'></center></pre></bdo></b><th id='3dYWY'></th></span></q></dt></tr></i><div id='3dYWY'><tfoot id='3dYWY'></tfoot><dl id='3dYWY'><fieldset id='3dYWY'></fieldset></dl></div>
  1. <tfoot id='3dYWY'></tfoot>
    <legend id='3dYWY'><style id='3dYWY'><dir id='3dYWY'><q id='3dYWY'></q></dir></style></legend>

      使用 MySQLi 进行 PHP 分页

      时间:2023-07-30
      <tfoot id='fKv6r'></tfoot>
      <i id='fKv6r'><tr id='fKv6r'><dt id='fKv6r'><q id='fKv6r'><span id='fKv6r'><b id='fKv6r'><form id='fKv6r'><ins id='fKv6r'></ins><ul id='fKv6r'></ul><sub id='fKv6r'></sub></form><legend id='fKv6r'></legend><bdo id='fKv6r'><pre id='fKv6r'><center id='fKv6r'></center></pre></bdo></b><th id='fKv6r'></th></span></q></dt></tr></i><div id='fKv6r'><tfoot id='fKv6r'></tfoot><dl id='fKv6r'><fieldset id='fKv6r'></fieldset></dl></div>
    1. <small id='fKv6r'></small><noframes id='fKv6r'>

            • <bdo id='fKv6r'></bdo><ul id='fKv6r'></ul>
                <tbody id='fKv6r'></tbody>

                <legend id='fKv6r'><style id='fKv6r'><dir id='fKv6r'><q id='fKv6r'></q></dir></style></legend>
                本文介绍了使用 MySQLi 进行 PHP 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在构建自己的 CMS.我有一个管理系统,我可以用它在数据库中插入帖子,显示帖子不是问题,但我不知道如何进行分页.

                I'm building my own CMS. I have an administration system made and I can insert posts in the database with it, showing posts isn't a problem, but I have no idea on how to do the pagination.

                这是我的查询:

                SELECT * FROM `posts` WHERE `status` != 'draft'
                

                推荐答案

                构建您的查询以具有 LIMIT

                结束SQL结果;

                SELECT * FROM posts WHERE status != 'draft' ORDER BY id ASC LIMIT <<offset>>, <<amount>>
                

                例如;

                SELECT * FROM posts WHERE status != 'draft' ORDER BY id ASC LIMIT 0, 10 #Fetch first 10
                SELECT * FROM posts WHERE status != 'draft' ORDER BY id ASC LIMIT 10, 10 #Fetch next 10
                

                阅读LIMIT

                您将需要 ORDER BY 您的主键,因为在没有 ORDER BY 子句的情况下,依赖 MySQL 给出的顺序在分页方面并不安全"(因为你可能会得到重复的行(在不同的页面上))

                You will need to ORDER BY your primary key, as it's not "safe" to rely on the order MySQL gives without the ORDER BY clause, in terms of pagination (as you may get duplicate rows (on different pages))

                这样的东西就足够了

                $intTotalPerPage = 10;
                $intPage = isset($_GET['page']) && ctype_digit($_GET['page']) ? (int) $_GET['page'] : 0;
                
                $strSqlQuery = "SELECT * FROM posts WHERE status != ? ORDER BY `id` ASC LIMIT ?, ?";
                $strStatus = 'draft';
                $intStart = ($intPage * $intTotalPerPage);
                $intLimit = $intTotalPerPage;
                $objDbLink = mysqli_connect("...");
                $objGetResults = mysqli_prepare($objDbLink, $strSqlQuery);
                mysqli_stmt_bind_param($objGetResults, 'sii',  $strStatus, $intStart, $intLimit);
                //Execute query and fetch
                //Display results
                
                $objTotalRows = mysqli_query("SELECT COUNT(id) AS total FROM posts WHERE status != 'draft'");
                $arrTotalRows = mysqli_fetch_assoc($objTotalRows);
                
                $intTotalPages = ceil($arrTotalRows['total'] / $intTotalPerPage);
                
                for ($i = 0; $i <= $intTotalPages; $i++) {
                    echo "<a href='?page=" . $i . "'>[" . $i . "]</a>&bsp;";
                }
                

                正如评论中所建议的,使用准备语句是一种很好的做法,通过 绑定参数

                As suggested in the comments it's good practice to use prepare statements, by binding parameters

                这篇关于使用 MySQLi 进行 PHP 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:实现mysqli连接的最佳实践/最实用的方法 下一篇:Mysqli 准备语句从数组动态构建 INSERT 查询

                相关文章

                最新文章

              • <legend id='I1nL3'><style id='I1nL3'><dir id='I1nL3'><q id='I1nL3'></q></dir></style></legend>
                  <bdo id='I1nL3'></bdo><ul id='I1nL3'></ul>
                <tfoot id='I1nL3'></tfoot>

                1. <small id='I1nL3'></small><noframes id='I1nL3'>

                  <i id='I1nL3'><tr id='I1nL3'><dt id='I1nL3'><q id='I1nL3'><span id='I1nL3'><b id='I1nL3'><form id='I1nL3'><ins id='I1nL3'></ins><ul id='I1nL3'></ul><sub id='I1nL3'></sub></form><legend id='I1nL3'></legend><bdo id='I1nL3'><pre id='I1nL3'><center id='I1nL3'></center></pre></bdo></b><th id='I1nL3'></th></span></q></dt></tr></i><div id='I1nL3'><tfoot id='I1nL3'></tfoot><dl id='I1nL3'><fieldset id='I1nL3'></fieldset></dl></div>