Code example for querying pages in a php + mysql database

Source: Internet
Author: User
Code example for querying pages in a php + mysql database

  1. /*

  2. * Php + mysql paging code
  3. *
  4. */

  5. $ SQL _TABL = "abc"; // table name

  6. $ Where_name = "id> 10"; // query condition
  7. $ Perpagenum = 3; // The number displayed on each page

  8. $ Total = mysql_fetch_array (mysql_query ("select count (*) AS count from $ SQL _TABL WHERE (". @ $ where_name .")"));

  9. $ SQL _count = $ total ['count']; // Obtain the number of returned data entries
  10. Unset ($ total); // deregister the variable $ total

  11. // Calculate the number of inaccurate pages. $ page_all_num_f is an integer and $ page_all_num_t is an exact value (which may be decimal places)

  12. $ Page_all_num_f = round ($ page_all_num_t = $ SQL _count/$ perpagenum, 0 );
  13. If ($ page_all_num_f <$ page_all_num_t) // calculate the correct number of pages.
  14. $ Page_all_num = $ page_all_num_f + 1;
  15. Else
  16. $ Page_all_num = $ page_all_num_f;

  17. If (is_numeric (@ $ _ GET ['P']) & amp; @ $ _ GET ['P']> 1 & amp; @ $ _ GET ['P'] <= $ page_all_num) // Obtain the GET parameter to determine the current page

  18. $ Page_num = $ _ GET ['P'];
  19. Else
  20. $ Page_num = 1;

  21. $ SQL _s_num = ($ page_num-1) * $ perpagenum; // calculates the number of data entries starting

  22. $ SQL _p = "LIMIT". $ SQL _s_num. ",". $ perpagenum; // Generate the database query code
  23. $ Result = mysql_query ("select * from $ SQL _TABL WHERE (". @ $ where_name. ")". $ SQL _p); // query data

  24. While ($ row = mysql_fetch_array ($ result) {// Main Loop

  25. // Output Content
  26. }

  27. // Pagination button

  28. For ($ I = 1; $ I <= $ page_all_num; $ I ++)
  29. {

  30. If (@ $ _ GET [name]! = Null) // Other GET parameters on the page

  31. $ P_n = "name =". @ $ _ GET [name]. "&";
  32. Else
  33. $ P_n = null;

  34. If ($ page_num = $ I) // emphasize the number of the current page

  35. $ P_flag = "class = \" flag \"";
  36. Else
  37. $ P_flag = null;

  38. Echo "$ I \ n ";

  39. }
  40. ?>

Recommended reading:
  • Php and ajax without refreshing paging code
  • Php article paging implementation code
  • Php limit paging code
  • Php paging class with multiple paging methods
  • Php paging code for the previous and next pages
  • Top 10 pages and top 10 pages of php paging code
  • Example of simple php paging code
  • A good php paging code
  • One paging function: next page on the previous page
  • A handy php paging class
  • Php long article paging code
  • A Practical php paging class
  • Quick php paging

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.