当前位置:K88软件开发文章中心编程语言PHPPHP01 → 文章内容

php pdo分页同时支持mysql和access

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2019-1-3 0:56:31

:2010-08-03 10:49:00

access是不支持limit分页的,想同的关键,问题马上就解决了。

看注释应该很容易理解,access的分页sql从asp json 的google code上来的 

<?php/** //分页类 */class Page{ //字段属性 public $param; //分页参数 public $pagesize; //每页大小 public $sql;//当数据库为access,无效 public $conn; //设置数据库类型 public $dbtype; //当数据库为access配置项 public $tbname;//表名 public $prikey;//主键 //public $url; public function __set($name,$value){  $this->$name = $value; } //初始化 public function __construct(){  //$this->conn=new mysqli("localhost","root","147258","empirecms");  //$this->conn->set_charset('utf8'); } //销毁 public function __destruct(){ } //开始 public function getStart(){  //$param=$this->param;  $param=(isset($_GET[$this->param])) ? $_GET[$this->param] : 1;  return ($param-1) * $this->pagesize; } //生成SQL语句 //mysql public function getMysqlSql(){  return $this->sql . " limit " . $this->getStart() ."," .$this->pagesize . "";  //return "test"; } //access的查询语句,参考json asp google public function getAccessSql(){  $end_n=$this->getStart() + $this->pagesize;  if($end_n>$this->pagesize){   $accsql = "SELECT TOP ". $this->pagesize ." * FROM [" . $this->tbname . "] as a where Not Exists(Select * From (Select Top ". $this->getStart() ." * From [" . $this->tbname . "] order by [" . $this->prikey . "] DESC) b Where b.[" . $this->prikey . "]=a.[" . $this->prikey . "] ) ORDER BY a.[" . $this->prikey . "] DESC" ;  }else{   $accsql = "SELECT TOP ". $this->pagesize ." * FROM [" . $this->tbname . "] ORDER BY [" . $this->prikey . "] DESC" ;  }  return $accsql; } //获取记录 public function getRecordSet(){  switch($this->dbtype){   case "ACCESS":    $ssql=$this->getAccessSql();    break;   case "MYSQL":    $ssql=$this->getMysqlSql();    break;  }  $rs1=$this->conn->query($ssql);  return $rs1; } //获取总页数 public function getPageCount(){  return $this->getRecordCount() / $this->pagesize; } //获取总记录数 public function getRecordCount(){  $rs=$this->conn->query("select count(*) from [" .$this->tbname ."]");  $row=$rs->fetchColumn();  return $row; } //获取分页导航 public function getPageTool(){  $pagecount= round($this->getPageCount());  $param=(isset($_GET[$this->param])) ? $_GET[$this->param] : 1;  if($param<=1){   echo "首页 上一页 ";  }else{   echo "<a href='?".$this->param."=1'>首页</a> <a href='?".$this->param."=".($param-1)."'>上一页</A> ";  }  if($param>=$pagecount){   echo "下一页 尾页";  }else{   echo "<A href='?".$this->param."=".($param+1)."'>下一页</A> <A href='?".$this->param."=".$pagecount."'>尾页</A>";  } }}?>


php pdo分页同时支持mysql和access