时间:2021-07-01 10:21:17 帮助过:19人阅读
//构造函数(连接数据库);
public function __construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("连接失败".mysql_errno());
}
//设置字符编码;
mysql_set_charset('utf8');
mysql_select_db($this->dbname,$this->conn);
}
//执行dql语句
public function execute_dql($sql){
$res=mysql_query($sql,$this->conn) or die(mysql_errno());
return $res;
}
//执行dql语句,但是返回的是一个数组
public function execute_dql2($sql){
$arr=array();
$res=mysql_query($sql,$this->conn) or die(mysql_errno());
$i=0;
//把$res的东西给$arr
while($row=mysql_fetch_array($res)){
$arr[$i]=$row;
$i++;
}
//这里可以马上关闭;
mysql_free_result($res);
return $arr;
}
//执行dml语句
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn) or die(mysql_errno());
if(!$b){
return 0;
}else{
if(mysql_affected_rows($this->conn)>0){
return 1;//表示只能执行成功
}else {
return 2;//表示没有行受到影响;
}
}
}
//封装
//考虑分页情况的查询
//$sql="select * from where 表名 limit 0,6 ";
//$sql="select count(*) from 表名";
public function execute_dql_fenye($sql1,$sql2,$fenyePage){
//得到分页的数据
$res=mysql_query($sql1,$this->conn) or die (mysql_errno());
//$res=>array();
//将数据转移到数组
$arr=array();
while($row=mysql_fetch_assoc($res)){
$arr[]=$row;
}
//释放资源
mysql_free_result($res);
//把数组赋值给$fenyePage
$fenyePage->res_array=$arr;
$res2=mysql_query($sql2,$this->conn) or die (mysql_errno());
if($row=mysql_fetch_row($res2)){
$fenyePage->pageCount=ceil($row[0]/$fenyePage->pageSize);
$fenyePage->rowCount=$row[0];
}
mysql_free_result($res2);
//把导航信息也封装到$fenyePage中;
//显示上一页和下一页
$navigate="";
if($fenyePage->pageNow>1){
$prePage=$fenyePage->pageNow-1;
$navigate="gotoUrl}?pageNow=$prePage'>上一页";
}
if($fenyePage->pageNow<$fenyePage->pageCount){
$nextPage=$fenyePage->pageNow+1;
$navigate.="gotoUrl}?pageNow=$nextPage'>下一页";
}
//显示首页和末页
$navigate.= "gotoUrl}?pageNow=1'>首页";
$navigate.= "gotoUrl}?pageNow=$fenyePage->pageCount'>末页";
//显示当前页和共有多少页
$navigate.= "当前页{$fenyePage->pageNow}/总共{$fenyePage->pageCount}页";
//以下显示多少页
$page_whole=2;
$start=floor(($fenyePage->pageNow-1)/$page_whole)*$page_whole+1;
$index=$start;
$min=floor(($fenyePage->pageCount-1)/$page_whole)*$page_whole;
//向前整体翻页;
//当前pageNow在10内,就没有翻页;
if($fenyePage->pageNow>$page_whole){
$navigate.= "gotoUrl}?pageNow=".($start-1)."'> <<";
}
//如何定义start (1到10)(floor(pageNow-1)/10)*10+1; (11到20)(floor(pageNow-1/10))*10+1
if($fenyePage->pageNow<=$min){
for($start;$start<$index+$page_whole;$start++){
$navigate.="gotoUrl}?pageNow=$start'>[$start]";
}
}else {
for ($start;$start<=$fenyePage->pageCount;$start++){
$navigate.="gotoUrl}?pageNow=$start'>[$start]";
}
}
//向后整体翻页
//向后翻页条显示条件;
//
// $min=floor($pageCount-1/$page_whole)
//
// if($pageNow<=$min*$pagewhole)
//
if($fenyePage->pageNow<=$min){
$navigate.= "gotoUrl}?pageNow=$start'> >> ";
}
$fenyePage->navigate=$navigate;
}
//关闭连接方式
public function close_connect(){
if(!empty($this->conn)){
mysql_close($this->conn);
}
}
}
?>