我是新手,希望有人能帮助我,我有一个 mysql 数据库,其中包含 4 个字段“id、日期、名称、评论”我有以下 php 文件:
<html>
<head>
<title>Paging Using PHP</title>
</head>
<body>
<?php
$dbhost = 'localhost';
$dbuser = 'USERNAME';
$dbpass = 'PASSWORD';
$rec_limit = 5;
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('DATABASE_NAME');
/* Get total number of records */
$sql = "SELECT count(id) FROM comments ";
$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 * FROM comments ORDER BY id DESC";
"FROM comments ".
"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 "<strong>Date :{$row['date']} </strong><br> ".
"<strong>Name : {$row['name']} </strong><br> ".
"Comment : {$row['comment']} <br> ".
"<br><br>";
}
if( $page > 0 )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Previous Page</a> |";
echo "<a href=\"$_PHP_SELF?page=$page\">Next Page</a>";
}
else if( $page == 0 )
{
echo "<a href=\"$_PHP_SELF?page=$page\">Next Page</a>";
}
else if( $left_rec < $rec_limit )
{
$last = $page - 2;
echo "<a href=\"$_PHP_SELF?page=$last\">Previous Page</a>";
}
mysql_close($conn);
?>
这一切都很好,除了我希望它每页显示 5 个结果之外,有人可以告诉我我需要更改什么,如何更改以及在哪里更改?非常感谢