如何使用PHP和MYSQL实现分页

时间:2016-07-05 14:13:10

标签: php mysql php-7 php-5.5 php-5.6

我正在尝试使用以下代码使用PHP和MySQL实现分页。

我已从互联网上获取帮助并更改了代码,但无法获取页面变量的值,因此代码无法正常工作

你能告诉我程序中的错误在哪里或者我如何获得页面变量值。该程序使用PHP和MYSQL db实现分页很简单。 代码如下。

<html>

<head>
  <title>Paging Using PHP</title>
</head>

<body>
  <?php
     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = 'root123';

     $rec_limit = 10;
     $conn = mysql_connect($dbhost, $dbuser, $dbpass);

     if(! $conn ) {
        die('Could not connect: ' . mysql_error());
     }
     mysql_select_db('test_db');

     /* Get total number of records */
     $sql = "SELECT count(cinno) FROM register_data ";
     $retval = mysql_query( $sql, $conn );

     if(! $retval ) {
        die('Could not get data: ' . mysql_error());
     }
     $row = mysql_fetch_array($retval, MYSQL_NUM );
     $rec_count = $row[0];

     if( isset($_GET{'page'} ) ) {
        $page = $_GET{'page'} + 1;
        $offset = $rec_limit * $page ;
     }else {
        $page = 0;
        $offset = 0;
     }

     $left_rec = $rec_count - ($page * $rec_limit);
     $sql = "SELECT cinno ". 
        "FROM register_data ".
        "LIMIT $offset, $rec_limit";

     $retval = mysql_query( $sql, $conn );

     if(! $retval ) {
        die('Could not get data: ' . mysql_error());
     }

     while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
        echo "Cin No :{$row['cinno']}  <br> ";

     }

      echo "$page";
     if( $page > 0 ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a> |";
        echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
     }else if( $page == 0 ) {
        echo "<a href = \"$_PHP_SELF?page = $page\">Next 10 Records</a>";
     }else if( $left_rec < $rec_limit ) {
        $last = $page - 2;
        echo "<a href = \"$_PHP_SELF?page = $last\">Last 10 Records</a>";
     }

     mysql_close($conn);
    ?>

   </body>
</html>

2 个答案:

答案 0 :(得分:1)

我通过查找$total_page然后比较$page == $total_page来判断最后一页的情况。 $left_rec方法并不适合。

// $left_rec = $rec_count - ($page * $rec_limit); not always give correct result
        $total_page = ceil($rec_count/$rec_limit);

ceil()是一个php函数,通过向上舍入返回下一个最高integer值。欲了解更多信息,请阅读http://php.net/manual/en/function.ceil.php

此外,您的查询字符串中还有空格,这就是为什么您无法获取页面值而您无需使用$_PHP_SELF就可以编写

 echo "<a href = '?page=$page'>Next 10 Records</a>"; or  echo "<a href = 'index.php?page=$page'>Next 10 Records</a>"; if the code is in index.php

//使用完整代码

<html>

<head>
  <title>Paging Using PHP</title>
</head>
<body>
  <?php
     $dbhost = 'localhost';
     $dbuser = 'root';
     $dbpass = 'root123';
     $rec_limit = 10;
     $conn = mysql_connect($dbhost, $dbuser, $dbpass);

     if(!$conn ) {
        die('Could not connect: ' . mysql_error());
     }
     mysql_select_db('test_db');

     /* Get total number of records */
     $sql = "SELECT count(cinno) FROM register_data";
     $retval = mysql_query( $sql, $conn );

     if(!$retval ) {
        die('Could not get data: ' . mysql_error());
     }
     $row = mysql_fetch_array($retval, MYSQL_NUM );
    $rec_count = $row[0];

     if(isset($_GET['page'] ) ) {
                $page = $_GET['page'];
        $offset = $rec_limit * ($page - 1);
     }else {
        $page = 1;
        $offset = 0;
     }

        // $left_rec = $rec_count - ($page * $rec_limit); not always give correct result
        $total_page = ceil($rec_count/$rec_limit);
      $sql = "select cinno from register_data LIMIT $offset, $rec_limit";
     $retval = mysql_query( $sql, $conn );

     if(! $retval ) {
        die('Could not get data2: ' . mysql_error());
     }

     while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
        echo "Cin No :{$row['cinno']}  <br> ";

     }

         if( $page >1 && $page != $total_page) {
            $last = $page - 1;
            $page++;            
            echo "<a href = '?page=$last'>Last 10 Records</a> |";
            echo "<a href = '?page=$page'>Next 10 Records</a>";
         }else if( $page == 1 ) {
             $page++;
            echo "<a href = '?page=$page'>Next 10 Records</a>";
         }elseif($page == $total_page){
              $last = $page - 1;
              echo "<a href = '?page=$last'>Last 10 Records</a>";
             }



     mysql_close($conn);
    ?>

   </body>
</html>

也请使用mysqli或PDO(更安全);

答案 1 :(得分:0)

$_PHP_SELF听起来不正确;您应该使用$_SERVER['PHP_SELF']或字符串${_SERVER['PHP_SELF']}