php实现读取mySQLl数据库并进行最简单分页

<html>

<?php
header("Content-type:text/html;charset=utf-8");//中文防止乱码
error_reporting(0);
$page=$_GET['p'];
//$pageSize='5';

$host="127.0.0.1";
$username="root";
$password="";
$db="test";
$conn=mysql_connect($host,$username,$password);
if(!$conn)
{
echo "数据库连接失败";
exit;
}
mysql_select_db("test");
mysql_query("set names utf8");
$sql="SELECT * FROM exam LIMIT"." " .($page-1)*10 .",10";//每个页面读取10条数据
//$sql="SELECT * FROM exam LIMIT ".($page-1)*$pageSize.",$pageSize";
$result=mysql_query($sql);
while($row=mysql_fetch_assoc($result))
{
echo "<table border=1 cellspacing=0 width=40%>";
echo ($row['id']);
echo '&nbsp';
echo ($row['name']);
echo "</table>";
}
mysql_free_result($result);
mysql_close($conn);


$page_banner = "<a href=" .$_SERVER['PHP_SELF'].'?p='.($page-1). ">上一页</a>";

$page_banner .= "<a href=" .$_SERVER['PHP_SELF'].'?p='.($page+1). ">下一页</a>";
echo $page_banner;

?>
</html>

注释:test 数据库下的exam表  。

posted @ 2016-09-06 18:58  Freeloop_嘉  阅读(471)  评论(0编辑  收藏  举报