我正在尝试分页工作,除了分页以外,一切正常。php mysql html分页不起作用
我正在使用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条记录?
在此先感谢。
不是'$ _GET ['page']'而不是'$ _GET {'page'}'? – andrewsi
不要使用'mysql_ *'函数,它们已经过时。改为使用[PDO](http://php.net/manual/en/book.pdo.php)或[mysqli](http://php.net/manual/en/book.mysqli.php)。 –