Home php教程 php手册 PHP读取mysql数据库数据并实现分页实例代码

PHP读取mysql数据库数据并实现分页实例代码

Jun 13, 2016 am 10:40 AM
mysql php code point Pagination Example accomplish data database read

PHP读取mysql数据库数据并实现分页实例代码如下:

  1.  //连接数据库  
  2.  $db=mysql_connect("localhost","root","root");  
  3.  mysql_select_db("gaoge",$db);  
  4.  //设定每一页显示的记录数  
  5.  $pagesize=3;  
  6.  
  7.  //取得记录总数,计算总页数用  
  8.  $res=mysql_query("select   count(*)   from   image   "   ,$db);  
  9.  $myrow   =   mysql_fetch_array($res);
  10.  //$myrow[o]为数据库里总记录数目之和
  11.  $numrows=$myrow[0];
  12.  //echo "total num is:";
  13.  //echo $numrows;
  14. // echo
    ;
  15.  //计算总页数  
  16.  $pages=intval($numrows/$pagesize);
  17.  
  18.  if   ($numrows%$pagesize)  
  19.  $pages ;
  20. // echo "total pages is:";
  21.  //echo $pages;
  22.   //echo
    ;
  23.  //判断页数设置与否,如无则定义为首页
  24.  
  25. if (isset($_GET[page])){
  26.  //echo "page exist";
  27.  $page = $_GET[page];
  28.  //echo "enter if ";
  29. }
  30. else{
  31.  //echo "page not exist";
  32.  $page = 1;
  33. }
  34.  
  35. //echo "page is:" ;
  36. // echo $page;
  37. // echo
    ;
  38.  //计算记录偏移量  
  39.  $offset=$pagesize*($page-1);
  40. // echo "offset is:" ;
  41.  echo $offset;
  42.  //取记录  
  43.  $res=mysql_query("select   *   from   image   order   by   id   desc   limit   $offset,$pagesize"   ,$db);  
  44.  //循环显示记录  
  45.  if   ($myrow   =   mysql_fetch_array($res))  
  46.  {  
  47.  $i=0;  
  48.  ?>    
  49.  
  50.    
  51.    
  52.    
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  do   {  
  59.  $i ;  
  60.  ?>  
  61.  
  62.   
  63.  
  64.   
  65.  
  66.  
  67.  
  68.   
  69.  
  70.   
  71.  
  72.  }  
  73.  while   ($myrow   =   mysql_fetch_array($res));  
  74.  echo   "
  75. ID 图片名称 上传人姓名 上传时间 图片效果
      PHP读取mysql数据库数据并实现分页实例代码
    "   ;  
  76.  }  
  77.  //显示总页数  
  78.  echo   "
    共有".$pages."页(".$page."/".$pages.")
    ";  
  79.  //显示分页数  
  80.  for   ($i=1;$i
  81.  echo   "第".$i   ."页     ";
  82.  //echo "page is:";
  83.  //echo "$page";
  84.  
  85. // echo   "
    ";  
  86.  //显示转到页数  
  87.  echo   "
       ";  
  88.  //计算首页、上一页、下一页、尾页的页数值  
  89.  $first=1;  
  90.  $prev=$page-1;  
  91.  $next=$page 1;  
  92.  $last=$pages;  
  93.   
  94.  echo   "首页     ";
  95.   //echo "page is:";
  96.  //echo "$page";
  97.  echo   "上一页     ";   
  98.     
  99.  echo   "下一页     ";  
  100.  echo   "尾页     ";   
  101.    
  102.  echo   "";  
  103.  echo   "
";  
  •  ?>
  • 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

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    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)

    PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

    Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

    PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

    I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

    Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

    Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

    Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

    Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

    The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    See all articles