PHP reads mysql database data and implements paging example code_PHP tutorial

WBOY
Release: 2016-07-13 17:41:35
Original
1054 people have browsed it

PHP reads mysql database data and implements paging example code as follows:

  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. //Get 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] is 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, it is defined as the home page
  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 record bias Offset
  35. $offset=$pagesize*($page-1);
  36. // echo "offset is:" ;
  37. echo $offset;
  38. //Get records
  39. $res=mysql_query("select * from image order by id desc limit $offset,$pagesize" ,$db);
  40. //Loop display records
  41. if ($myrow = mysql_fetch_array($res))
  42. {
  43. $i=0;
  44. ?>
  45. < ;table cellspacing=0 bordercolordark=#FFFFFF width="95%" bordercolorlight=#000000 border=1 align="center" cellpadding="2">
  46.   ID
  47.   Picture name
  48.   Uploader name
  49. Upload time
  50. Picture effect
  51. do {
  52. $i ;
  53. ?>
  54. < ?php echo $myrow[2];?>
  55. }
  56. while ($myrow = mysql_fetch_array ($res));
  57. echo "" ;
  58. }
  59. //Display the total number of pages
  60. echo "
    There are ".$pages." pages (".$page."/".$pages.")
    ";
  61. //Display the number of pages
  62. for ($i=1;$i<=$pages;$i)
  63. echo "< ;a href=pages.php?page=".$i.">Page ".$i ." ";
  64. //echo "page is:";
  65. //echo "$page";
  66. // echo "
    ";
  67. //Display the page number
  68. echo "
    ";
  69. //Calculate the page values ​​of the first page, previous page, next page and last page
  70. $ first=1;
  71. $prev=$page-1;
  72. $next=$page 1;
  73. $last=$pages;
  74. echo "Homepage ";
  75. //echo "page is:";
  76. //echo "$page";
  77. echo "Previous page ";
  78. echo "Next page ";
  79. echo "Last page ";
  80. echo "";
  81. echo "
";
  • ?>
  • www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486122.htmlTechArticlePHP reads mysql database data and implements paging example code as follows: ?php //Connect to the database $db=mysql_connect( "localhost","root","root"); mysql_select_db("gaoge",$db); //Set each page to display...
    source:php.cn
    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
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!