PHP reads mysql database data and implements paging instance code

Source: Internet
Author: User
The code for PHP to read mysql database data and implement paging instances is as follows: & lt ;? Php & nbsp; // Connect to database & nbsp; $ dbmysql_connect (localhost, root, root); & nbsp; & nbsp; mysql_select_db (gaoge, $ db) PHP reads mysql database data and implements the following code for paging instance:

  1. // Connect to the database
  2. $ Db = mysql_connect ("localhost", "root", "root ");
  3. Mysql_select_db ("gaoge", $ db );
  4. // Set the number of records displayed on each page
  5. $ Pagesize = 3;
  6.  
  7. // Obtain the total number of records and calculate the total number of pages
  8. $ Res = mysql_query ("select count (*) from image", $ db );
  9. $ Myrow = mysql_fetch_array ($ res );
  10. // $ Myrow [o] indicates the sum of the total number of records in the database
  11. $ Numrows = $ myrow [0];
  12. // Echo "total num is :";
  13. // Echo $ numrows;
  14. // Echo
    ;
  15. // Calculate the total number of pages
  16. $ Pages = intval ($ numrows/$ pagesize );
  17.  
  18. If ($ numrows % $ pagesize)
  19. $ Pages;
  20. // Echo "total pages is :";
  21. // Echo $ pages;
  22. // Echo
    ;
  23. // Determine whether the page number is set or not. if not, the page number is defined as the homepage.
  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. // Calculate the record offset
  39. $ Offset = $ pagesize * ($ page-1 );
  40. // Echo "offset is :";
  41. Echo $ offset;
  42. // Retrieve records
  43. $ Res = mysql_query ("select * from image order by id desc limit $ offset, $ pagesize", $ db );
  44. // Display records cyclically
  45. If ($ myrow = mysql_fetch_array ($ res ))
  46. {
  47. $ I = 0;
  48. ?>
  49.  
  50.  
  51.  
  52.  
  53. $ I;
  54. ?>
  55.  
  56.  
  57.  
  58.  
  59. "/>
  60.  
  61.  
  62. While ($ myrow = mysql_fetch_array ($ res ));
  63. Echo"
  64. Do {
  65. }
  66. ID Image name Uploaded person name Upload time Image Effects
    ";
  67. }
  68. // Display the total number of pages
  69. Echo "total". $ pages. "page (". $ page. "/". $ pages .")
    ";
  70. // Display the number of pages
  71. For ($ I = 1; $ I <= $ pages; $ I)
  72. Echo "page". $ I ";
  73. // Echo "page is :";
  74. // Echo "$ page ";
  75.  
  76. // Echo"
    ";
  77. // Display to page number
  78. Echo "";
  79. Echo "";
  80. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.