SqlHelper.class.php类
<?php
//工具类,完成对数据库的操作
class SqlHelper{
private $conn;
private $host="localhost";
private $user="root";
private $password="";
private $db="test1";
function __construct(){
//连接数据库
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("对不起,连接数据库失败<br/>错误原因:".mysql_error());
}
mysql_select_db($this->db,$this->conn);//选择数据库
mysql_query("set names utf8");
}
public function execute_dql($sql){
//执行数据库dql语句,即查询操作,返回的是结果集,集合
$res=mysql_query($sql,$this->conn) or die("查询失败,失败原因".mysql_error());
return $res;
}
/*
* execute_dql2方法将从数据中查询获取到的结果集放到数组,方便关闭结果集资源(资源需要在哪里取到在哪里释放)
* */
public function execute_dql2($sql){
$arr=array();//先声明一个数组
//执行数据库dql语句,即查询操作,返回的是结果集,集合
$res=mysql_query($sql,$this->conn) or die("查询失败,失败原因".mysql_error());
//使用while循环取出结果集的数据放入数组把$res=>$arr
$i=0;
while ($row=mysql_fetch_assoc($res)){
$arr[$i++]=$row;
}
//这里就可以马上把资源$res释放
mysql_free_result($res);
return $arr;
}
//在这里就实现了对CRUD的完全封装
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
//return 0; //运行失败
echo "对不起,操作失败";
}else{
//返回的是受影响行数
if(mysql_affected_rows($this->conn)>0){
//return 1; //运行成功
echo "操作成功!";
}else{
//return 2; //成功,但没有影响行数
echo "操作成功,但是行数没有受到影响";
}
}
//mysql_close($this->conn);//关闭连接
}
//关闭连接的方法,当连接不为空的时候再关闭,避免某些情况下连接关闭,可执行多条语句
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>
EmpService.class.php类,获取数据库查询的结果,处理结果用于分页的数据显示操作
<?php
class EmpService{
//定义函数,获取共有多少页,需要传递一个$pageSize参数
function getpageCount($pageSize){
//编写sql语句,查询数据库的数据
$sql="select count(id) from tb_user";
//创建sqlHelper对象,调用查询方法
$sqlHelper=new SqlHelper();
$res=$sqlHelper->execute_dql($sql);
//对查询结果进行判断,取出查询的结果,计算出pageCount
if($row=mysql_affected_rows($res)){
//得到共有多少条数据记录
$rowCount=$row[0];
$pageCount=ceil($rowCount/$pageSize);
}
//释放资源关闭连接
mysql_free_result($res);
$sqlHelper->close_connect();
//返回共有多少页
return $pageCount;
}
//定义函数,获取每页显示的数据
function getEmpListByPage($pageNow,$pageSize){
//编写sql语句,查询具体数据,因为页面要进行分页处理,这里传sql语句的时候要限制条件(从第几条数据开始取,每页显示多少条数据)
$sql="select * from tb_user limit ".($pageNow-1)*$pageSize.",$pageSize'";
$sqlHelper=new SqlHelper();
//这里获取的$arr是一个二维数组
$arr=$sqlHelper->execute_dql2($sql);
//释放资源,关闭连接
$sqlHelper->close_connect();
return $arr;
}
}
empList.php分页的页面显示
<html>
<head>
<meta
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
require_once 'AdminService.class.php';
/*
分页
*/
$pageSize=6;//自定义,每页显示3条数据
//$rowcount=0;//共有多少条记录,需要从数据中查询
//$pageCount=0;//共有几页,需要计算
$pageNow=1;//显示第几页,变化量,根据用户点击超链接确定,首先要先判断是否有pageNow发送,没有就显示默认显示第一页
if(!empty($_GET['pageNow'])){
$pageNow=$_GET['pageNow'];
}
$empService=new EmpService();
//获取共有多少页,需调用empService.class.php的getpageCount方法,获取该方法的返回值
$pageCount=$empService->getpageCount($pageSize);
//获取每页显示的数据,需调用empService.class.php的getEmpListByPage方法,获取该方法的返回值
$rarr=$empService->getEmpListByPage($pageNow,$pageSize);
//将获取到的数据写入表格
echo "<table border='1' width='700px'>";
echo "<tr><th>ID</th><th>姓名</th><th>等级</th><th>电子邮箱</th><th>薪资</th><th>操作</th></tr>";
if($arr){
//这里要通过数组取
for($i=0;$i<count($arr);$i++){
//将数组中取出的每一列放到$row中
$row=$arr[$i];
echo "<tr><td>{$row['Id']}</td><td>{$row['name']}</td>".
"<td>{$row['grade']}</td><td>{$row['email']}</td><td>{$row['salary']}</td>".
"<td align='center'><a href='#'>修改用户</a> | <a href='#'>删除用户</a></td></tr>";
}
/*
* 此方法是直接使用从数据库查询结果传递过来的结果集,需要在这里关闭结果集
* while ($row=mysql_fetch_assoc($res2)){
echo "<tr><td>{$row['Id']}</td><td>{$row['name']}</td>".
"<td>{$row['grade']}</td><td>{$row['email']}</td><td>{$row['salary']}</td>".
"<td align='center'><a href='#'>修改用户</a> | <a href='#'>删除用户</a></td></tr>";
} */
}
echo "</table>";
//主要显示页码,用户点击页码是对该页面发出指令,所以页码连接主要是该页面
/* for($i=1;$i<=$pageCount;$i++){
echo "<a href='empList.php?pageNow=$i'>{$i} </a>";
} */
//显示上一页下一页,总页数,跳转制定页面
echo"<a href='empList.php?pageNow=1'>首页</a> ";
if($pageNow>1){
//上一页
$perPage=$pageNow-1;
echo "<a href='empList.php?pageNow=$perPage'>上一页</a> ";
}
if($pageNow<$pageCount){
//下一页
$nextPage=$pageNow+1;
echo "<a href='empList.php?pageNow=$nextPage'>下一页</a> ";
}
echo"<a href='empList.php?pageNow=$pageCount'>最后一页</a>";
echo " 共有{$pageCount}页 ";
//跳转到制定页面,使用表单形式实现
echo "<form action='empList.php'>";
echo "跳转到<input type='text' name='pageNow'/>页 ";
echo "<input type='submit' value='GO'/>";
echo "</form>";
//关闭资源
/* mysql_free_result($res1);
mysql_close($conn); */
?>
</body>
</html>
学到这一步了,记录一下,有待用更好的方法完善