PHP mysql HTML分页不工作


php mysql html pagination not working

我试图得到分页工作,一切工作良好,除了分页。

我正在使用PHP, HTML和MYSQL。我可以取回记录,但网页上的所有记录显示,我只是想限制它显示每页10。

我不知道我做错了什么。

<html>
<link rel="stylesheet" type="text/css" href="main.css">
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost:3036';
$dbuser = 'useer';
$dbpass = 'passwoord';
$rec_limit = 10;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
mysql_select_db('disks');
/* Get total number of records */
$sql = "SELECT count(id) FROM hdd ";
$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;
}
        echo '<h3>',Table,'</h3>';
$left_rec = $rec_count - ($page * $rec_limit);
$sql = "SELECT cust, manu, model, serial, capacity, firmware, deviceid, ataver, ltime, date, ourref, result FROM hdd";
       "FROM hdd".
       "LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Customer</th> <th>HDD Type</th> <th>Model</th> <th>Serial</th> <th>Size</th> <th>Firmware</th> <th>Device ID</th> <th>ATA Ver</th> <th>Manufactured On</th> <th>date</th> <th>ourref</th> <th>result</th></tr>';
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo '<tr>';
$i=0;
                        foreach($row as $key=>$value) {
 if($i==10) break;
                                echo '<td>',$value,'</td>';
                        }
                        echo '</tr>';
}
                echo '</table><br />';
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);
?>

我怎样才能让它每页只显示10条记录?

提前感谢。

我认为问题是与您的SQL查询。你有:

$sql = "SELECT cust, manu, model, serial, capacity, firmware, deviceid, ataver, ltime, date, ourref, result FROM hdd";
       "FROM hdd".
       "LIMIT $offset, $rec_limit";

实际位置:

$sql = "SELECT cust, manu, model, serial, capacity, firmware, deviceid, ataver, ltime, date, ourref, result FROM hdd ";
$sql .= "LIMIT $offset, $rec_limit";

我也同意约翰提到的。你有$_GET{'page'},它应该是$_GET['page']在两个地方

为什么使用$_GET{'page'}?将$_GET{'page'}更改为$_GET['page']。