PHP reads mysql database data and implements paging instance code _ PHP Tutorial

Source: Internet
Author: User
PHP reads mysql database data and implements paging instance code. The code for PHP to read mysql database data and implement paging instances is as follows :? Php connects to the database $ dbmysql_connect (localhost, root, root); mysql_select_db (gaoge, $ db); sets PHP to read mysql database data on each page and implements the following code for paging instances:

  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. // Obtain the total number of records and calculate the total number of pages
  7. $ Res = mysql_query ("select count (*) from image", $ db );
  8. $ Myrow = mysql_fetch_array ($ res );
  9. // $ Myrow [o] indicates the sum of the total number of records in the database
  10. $ Numrows = $ myrow [0];
  11. // Echo "total num is :";
  12. // Echo $ numrows;
  13. // Echo
    ;
  14. // Calculate the total number of pages
  15. $ Pages = intval ($ numrows/$ pagesize );
  16. If ($ numrows % $ pagesize)
  17. $ Pages;
  18. // Echo "total pages is :";
  19. // Echo $ pages;
  20. // Echo
    ;
  21. // Determine whether the page number is set or not. if not, the page number is defined as the homepage.
  22. If (isset ($ _ GET [page]) {
  23. // Echo "page exist ";
  24. $ Page = $ _ GET [page];
  25. // Echo "enter if ";
  26. }
  27. Else {
  28. // Echo "page not exist ";
  29. $ Page = 1;
  30. }
  31. // Echo "page is :";
  32. // Echo $ page;
  33. // Echo
    ;
  34. // Calculate the record offset
  35. $ Offset = $ pagesize * ($ page-1 );
  36. // Echo "offset is :";
  37. Echo $ offset;
  38. // Retrieve records
  39. $ Res = mysql_query ("select * from image order by id desc limit $ offset, $ pagesize", $ db );
  40. // Display records cyclically
  41. If ($ myrow = mysql_fetch_array ($ res ))
  42. {
  43. $ I = 0;
  44. ?>
  45. $ I;
  46. ?>
  47. While ($ myrow = mysql_fetch_array ($ res ));
  48. Echo"
  49. Do {
  50. }
  51. ID Image name Uploaded person name Upload time Image Effects
    "/>
    ";
  52. }
  53. // Display the total number of pages
  54. Echo"

    Total ". $ pages." page (". $ page."/". $ pages .")
    ";

  55. // Display the number of pages
  56. For ($ I = 1; $ I <= $ pages; $ I)
  57. Echo "page". $ I ";
  58. // Echo "page is :";
  59. // Echo "$ page ";
  60. // Echo"
    ";
  61. // Display to page number
  62. Echo "";
  63. Echo"

    ";
  64. ?>

Why :? Php // connect to the database $ db = mysql_connect ("localhost", "root", "root"); mysql_select_db ("gaoge", $ db ); // Set each page to display...

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.