我想添加一个简单的分页,但从 msql 更改为 mysqli 后,我似乎无法正确处理。我还不能理解它,我只是从它开始。我按照教程进行操作,但仍然遇到错误。
抱歉我缺乏知识,但我错过了什么?
<?php
$db = new mysqli("host", "username", "password", "mydatabase");
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$rec_limit = 10;
$sql = "SELECT COUNT(photo)FROM employees";
$retval = mysql_query( $sql, $db );
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 photo, link".
"FROM employees ".
"LIMIT $offset, $rec_limit";
$retval = mysql_query( $sql, $db );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
echo "<br /><br />";
echo '','<img src="/upload/' . $row->photo . '" border=0>';
echo '', $row->link;
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($db);
?>