Home Backend Development PHP Tutorial PHP and Mysql implement data paging display example

PHP and Mysql implement data paging display example

Mar 26, 2018 pm 02:16 PM
mysql php show

This article mainly shares with you examples of data paging display using php and Mysql, hoping to help everyone.

  1. To obtain the number of records in the result set, you can use the COUNT() function in the SELECT statement to obtain the number of records in the result set.

  2. Set the display per page The number of records assumes that the variable $PageSize is used to save the number of records displayed on each page. Its value is set by the user according to needs, and can be implemented directly through an assignment statement.

  3. To obtain the total number of pages, you can calculate the total number of pages through the two data $RecordCount and $PageSize$PageCount,

  4. How to display the records in page n Although the PageSize property can be used to control the number of records displayed on each page, but which records should be displayed? You can use the LIMIT clause in the SELECT statement to specify the range of query records. The usage method is as follows: SELECT * FROM table name LIMIT starting position, display the number of records. For example, to obtain the records in page $Page , you can use the following statement: SELECT * FROM table name LIMIT ($Page- 1) * $ PageSize, $ PageSize

  5. How to notify the script of the page number to be displayed You can tell the script the page number to display by passing parameters. Assume that the script for paging records is viewPage. php, and the link to pass parameters is as follows: http:// localhost/ viewPage. php? page= 2 The page parameter is used to specify the current page number. In viewPage.php, use the following statement to read the parameters:

<!doctype html><html lang="en"><head>
    <meta charset="utf-8" />
    <title>Document</title></head><body>
    <?php 
    header("content-type:text/html;charset=utf-8");        //获取当前页码
        $page=$_GET[&#39;page&#39;];        if($page==0){            $page=1;
        }        //设置每页最大能显示的数量
        $pagesize=3;        //连接数据库
        $conn=mysql_connect("localhost","root","root");
        mysql_select_db("test");
        mysql_query("set names utf-8");        if(!$conn){            die("mysql_connect_failed".mysql_connect_error());
        }        else 
            echo("connected succeed"."<br />");        //获取结果集的记录数
        $row=mysql_fetch_row(mysql_query("select count(1) from clerk"));        $recordcount=$row[0]; 




        //计算总页数
        if($recordcount==0)            $pagecount=0;        else if($recordcount<$pagesize ||$recordcount==$pagesize){                $pagecount=1;                //如果 记录 总数 量小 于 每页 显示 的 记录 数量, 则 只有 一页
            }        else if($recordcount%$pagesize==0){                $pagecount=$recordcount/$pagesize;                //如果 没有 余数, 则 页数 等于 总 记录 数量 除以 每页 显示 记录 的 数量
            }        else 
                $pagecount=(int)($recordcount/$pagesize)+1;                //取 记录 总数 量 不能 整除 每页 显示 记录 的 数量,
                // 则 页数 等于 总 记录 数量 除以 每页 显示 记录 数量 的 结果 取整 再加 1

        echo("当前页码:".$page."/".$pagecount."<br />");    ?>

    <table width="449" border="1">
        <tr>
            <td>员工姓名</td>
            <td>职务</td>
            <td>薪水</td>
        </tr>
    <?php 
    //循环显示当前页面的记录
    header("content-type:text/html;charset=utf-8");    echo $page;    //$sql="select * from clerk limit" .($page-1)*$pagesize.",".$pagesize;  //$page为当前页码
    $sql=($page-1)*$pagesize;    $result=mysql_query("select * from clerk limit {$sql},{$pagesize}");    while($row=mysql_fetch_row($result))
    {   

        echo("<tr />");        echo("<td>$row[0]</td>");        echo("<td>$row[2]</td>");        echo("<td>$row[3]</td>");        echo("<tr />");
    }
    mysql_close($conn);    //显示分页链接
    if($page==1){        echo("第一页");
    }    else
        echo("<a href=viewpage.php?page=1>第一页</a>");        //设置上一页连接

    if($page==1){         echo("上一页");
    }    else 
        echo("<a href=viewpage.php?page=".($page-1).">上一页</a>");        //设置下一页链接
    if($page==$pagecount){        echo("下一页");
    }    else 
        echo("<a href=viewpage.php?page=".($page+1).">下一页</a>");    //设置最后一页
    if($page==$pagecount){        echo("最后一页");
    }    else 
        echo("<a href=viewpage.php?page=".$pagecount.">最后一页</a>");    ?>
    </table></body></html>
Copy after login
Copy after login


PHP and Mysql implement data paging display example

  1. To get the number of records in the result set, you can use the COUNT() function in the SELECT statement to get the number of records in the result set.

  2. Set the number of records displayed on each page. It is assumed that the variable $PageSize is used. To save the number of records displayed on each page, its value can be set by the user according to needs, which can be achieved directly through assignment statements.

  3. To obtain the total number of pages, you can calculate the total number of pages through the two data $RecordCount and $PageSize$PageCount,

  4. How to display the records in page n Although the PageSize property can be used to control the number of records displayed on each page, but which records should be displayed? You can use the LIMIT clause in the SELECT statement to specify the range of query records. The usage method is as follows: SELECT * FROM table name LIMIT starting position, display the number of records. For example, to obtain the records in page $Page , you can use the following statement: SELECT * FROM table name LIMIT ($Page- 1) * $ PageSize, $ PageSize

  5. How to notify the script of the page number to be displayed You can tell the script the page number to display by passing parameters. Assume that the script for paging records is viewPage. php, and the link to pass parameters is as follows: http:// localhost/ viewPage. php? page= 2 The page parameter is used to specify the current page number. In viewPage.php, use the following statement to read the parameters:

<!doctype html><html lang="en"><head>
    <meta charset="utf-8" />
    <title>Document</title></head><body>
    <?php 
    header("content-type:text/html;charset=utf-8");        //获取当前页码
        $page=$_GET[&#39;page&#39;];        if($page==0){            $page=1;
        }        //设置每页最大能显示的数量
        $pagesize=3;        //连接数据库
        $conn=mysql_connect("localhost","root","root");
        mysql_select_db("test");
        mysql_query("set names utf-8");        if(!$conn){            die("mysql_connect_failed".mysql_connect_error());
        }        else 
            echo("connected succeed"."<br />");        //获取结果集的记录数
        $row=mysql_fetch_row(mysql_query("select count(1) from clerk"));        $recordcount=$row[0]; 




        //计算总页数
        if($recordcount==0)            $pagecount=0;        else if($recordcount<$pagesize ||$recordcount==$pagesize){                $pagecount=1;                //如果 记录 总数 量小 于 每页 显示 的 记录 数量, 则 只有 一页
            }        else if($recordcount%$pagesize==0){                $pagecount=$recordcount/$pagesize;                //如果 没有 余数, 则 页数 等于 总 记录 数量 除以 每页 显示 记录 的 数量
            }        else 
                $pagecount=(int)($recordcount/$pagesize)+1;                //取 记录 总数 量 不能 整除 每页 显示 记录 的 数量,
                // 则 页数 等于 总 记录 数量 除以 每页 显示 记录 数量 的 结果 取整 再加 1

        echo("当前页码:".$page."/".$pagecount."<br />");    ?>

    <table width="449" border="1">
        <tr>
            <td>员工姓名</td>
            <td>职务</td>
            <td>薪水</td>
        </tr>
    <?php 
    //循环显示当前页面的记录
    header("content-type:text/html;charset=utf-8");    echo $page;    //$sql="select * from clerk limit" .($page-1)*$pagesize.",".$pagesize;  //$page为当前页码
    $sql=($page-1)*$pagesize;    $result=mysql_query("select * from clerk limit {$sql},{$pagesize}");    while($row=mysql_fetch_row($result))
    {   

        echo("<tr />");        echo("<td>$row[0]</td>");        echo("<td>$row[2]</td>");        echo("<td>$row[3]</td>");        echo("<tr />");
    }
    mysql_close($conn);    //显示分页链接
    if($page==1){        echo("第一页");
    }    else
        echo("<a href=viewpage.php?page=1>第一页</a>");        //设置上一页连接

    if($page==1){         echo("上一页");
    }    else 
        echo("<a href=viewpage.php?page=".($page-1).">上一页</a>");        //设置下一页链接
    if($page==$pagecount){        echo("下一页");
    }    else 
        echo("<a href=viewpage.php?page=".($page+1).">下一页</a>");    //设置最后一页
    if($page==$pagecount){        echo("最后一页");
    }    else 
        echo("<a href=viewpage.php?page=".$pagecount.">最后一页</a>");    ?>
    </table></body></html>
Copy after login
Copy after login

PHP and Mysql implement data paging display example

The above is the detailed content of PHP and Mysql implement data paging display example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

See all articles