文章来源:
100素材网
更新时间:
2014-08-07 15:27:55
jsp分页 mysql分页 分页查询 php分页 jquery分页 php分页代码 php分页显示
下面是一个对数据库显示的内容进行分页的简单的例子。
<html> <head> <title>Paging Using PHP</title> </head> <body> <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $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(emp_id) FROM employee "; $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 emp_id, emp_name, emp_salary ". "FROM employee ". "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 "EMP ID :{$row['emp_id']} <br> ". "EMP NAME : {$row['emp_name']} <br> ". "EMP SALARY : {$row['emp_salary']} <br> ". "--------------------------------<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); ?>
浏览次数次
上一篇文章: php ajax图片上传代码
下一篇文章: PHP数组、数字数组、关联数组和多维数组