<?php
class Dbo{
protected $dbhost, $dbuser, $dbpwd, $dbname;
public $conn, $record, $rTmp, $rPointer, $rTotal, $sql;
public $pageSize, $currentPage, $pageTotal, $recordTotal, $oConnect;
function __construct($sql='', $pageSize=0, $currentPage=''){
$this->dbtype=DB_TYPE;
$this->dbhost=DB_HOST;
$this->dbuser=DB_USER;
$this->dbpwd=DB_PASSWORD;
$this->dbname=DB_NAME;
$this->dbcharset=DB_CHARSET;
$this->conn='';
$this->sql=$sql;
$this->record='';
$this->rTmp='';
$this->rArr=array();
$this->rPointer=0;
$this->rTotal=0;
$this->pageSize=$pageSize;
$this->currentPage=($currentPage=='')?$_GET["page"]:$currentPage;
$this->pageTotal=0;
$this->recordTotal=0;
$this->openDB();
if(!empty($sql))$this->qrySql($sql);
if($pageSize)$this->page();
}
private function openDB(){
$this->pdo=new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'GBK';")) or die("连接服务器失败:".$this->dbhost.':'.$this->dbuser);
}
public function runSql($sql){
// $this->pdo->query('set names GBK;');
$this->pdo->query($sql) or die("执行SQL语句出错[runSql]:".$sql);
}
public function qrySql($sql){
// $this->pdo->query('set names GBK;');
$this->record=$this->pdo->query($sql) or die("执行SQL语句出错[qrySql]:".$sql);
return $this->record;
}
public function rNext(){
$rArr=$this->record->fetch();
return $rArr;
}
public function result_one($sql){
// $this->pdo->query('set names GBK;');
$rTmp=$this->pdo->query($sql) or die("执行SQL语句出错[result_one]:".$sql);
$rArr=$rTmp->fetch();
return $rArr[0];
}
public function result_onea($sql){
$rTmp=$this->pdo->query($sql) or die("执行SQL语句出错[result_onea]:".$sql);
$rArr=$rTmp->fetch();
return $rArr;
}
public function result_all($sql){
$rTmp=$this->pdo->query($sql) or die("执行SQL语句出错[result_all]:".$sql);
$rArr=$rTmp->fetchAll();
foreach($rArr as $rArrTmp){
static $i=0;
$arr[$i]=$rArrTmp[0];
$i++;
}
return $arr;
}
public function fetch_all($sql){
$rTmp=$this->pdo->query($sql) or die("执行SQL语句出错[fetch_all]:".$sql);
$rArr=$rTmp->fetchAll();
return $rArr;
}
//通过sql语句获取字段总数
public function columnCount($sql){
$p=$this->pdo->prepare($sql);
$p->execute();
$columnCount=$p->columnCount() or die("执行SQL语句出错[columnCount]:".$sql);
return $columnCount;
}
public function getColumnName($tb){
$sql='SHOW COLUMNS FROM '.$tb;
$p = $this->pdo->prepare($sql) or die("执行SQL语句出错[getColumnName]:".$sql);
try {
if($p->execute()){
$raw_column_data = $p->fetchAll();
foreach($raw_column_data as $outer_key => $array){
foreach($array as $inner_key => $value){
if ($inner_key === 'Field'){
if (!(int)$inner_key){
$column_namesArr[] = $value;
}
}
}
}
}
return $column_namesArr;
}catch (Exception $e){
return $e->getMessage();
}
}
public function getColumnType($tb){
$select = $DB->query('SELECT COUNT(*) FROM fruit');
$meta = $select->getColumnMeta(0);
var_dump($meta);
$sql='SHOW COLUMNS FROM '.$tb;
$p = $this->pdo->prepare($sql) or die("执行SQL语句出错[getColumnName]:".$sql);
try {
if($p->execute()){
$raw_column_data = $p->fetchAll();
foreach($raw_column_data as $outer_key => $array){
foreach($array as $inner_key => $value){
if ($inner_key === 'Field'){
if (!(int)$inner_key){
$column_namesArr[] = $value;
}
}
}
}
}
return $column_namesArr;
}catch (Exception $e){
return $e->getMessage(); //return exception
}
}
/*
//mssql、mysql数据库通用分页
function page(){
$R=$this->record;
if ($R){
$this->recordTotal = $R->rowCount();
$this->pageTotal = ceil($this->recordTotal/$this->pageSize);
if ($this->currentPage>$this->pageTotal) $this->currentPage=$this->pageTotal;
if (!$this->currentPage||$this->currentPage<=0) $this->currentPage=1;
$begin = ( $this->currentPage-1 ) * $this->pageSize;
$ReArr=array();
$index=0;
if($this->pageSize>0) @mysql_data_seek($R,$begin);
while($ResultArr=@$R->fetch()){
if($this->pageSize>0){
if($index>$this->pageSize-1) break;
$ReArr[$index]=$ResultArr;
$index++;
}
}
return $ReArr;
}else{
return false;
}
}
*/
function page(){
$R=$this->record;
if ($R){
$this->recordTotal = $R->rowCount();
$this->pageTotal = ceil($this->recordTotal/$this->pageSize);
if ($this->currentPage>$this->pageTotal) $this->currentPage=$this->pageTotal;
if (!$this->currentPage||$this->currentPage<=0) $this->currentPage=1;
$begin = ( $this->currentPage-1 ) * $this->pageSize;
$this->record=$this->qrySql("$this->sql limit $begin, $this->pageSize") or die("分页时打开表失败1");
}else{
return false;
}
}
function pagetip(){
if(empty($this->currentPage) or $this->currentPage<0) $this->currentPage=1;
if($this->currentPage>$this->pageTotal) $this->currentPage=$this->pageTotal;
$links="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if(empty($_SERVER['QUERY_STRING'])){ //判断首次进入分页状态时是否存在参数
$links=$links."?page=";
}else{
$links=preg_replace('(\\&?page=[0-9]+|page=[0-9]+\\&?)',"",$links);
$links=$links."&page=";
}
echo "总".$this->recordTotal."条 第".$this->currentPage."/".$this->pageTotal."页 ";
echo "<a style=color:#000000 href={$links}1>首页</a> ";
echo "<a style=color:#000000 href={$links}".($this->currentPage-1).">上一页</a> ";
echo "<a style=color:#000000 href={$links}".($this->currentPage+1).">下一页</a> ";
echo "<a style=color:#000000 href={$links}$this->pageTotal>尾页</a>";
echo " <select name=menu1 onchange='window.location=this.options[this.selectedIndex].value'>";
for($x=1;$x<=$this->pageTotal;$x++){
if($x==$this->currentPage){
echo "<option selected=selected value={$links}$x>".$x."</option>" ;
}else{
echo "<option value={$links}$x>".$x."</option>" ;
}
}
echo " </select>";
}
}
/*
//执行语句获取记录
$dbo=new Dbo("select * from news order by taxis desc");
while($array=$dbo->rNext()){$id=$array['id']}
//执行语句检查是否存在记录
$dbo=new Dbo();
$chkExists=$dbo->result_one("select count(id) from newssort where fcode=''")
if($chkExists){};
//分页调用方式1(可用于PHP+MSSQL,使用mssql、mysql数据库通用分页函数时):
$pageSize=10;
$Pdbo=new Dbo("select * from news order by taxis desc,id desc", $pageSize);
$rArr=$Pdbo->page();
for($i=0; $i<count($rArr); $i++){
$array=$rArr[$i];
$id=$array["id"];
$listNum=($Pdbo->currentPage-1)*$pageSize+$i+1;
}
//分页调用方式2:
$pageSize=10;
$Pdbo=new Dbo("select * from news order by taxis desc,id desc", $pageSize);
while($array=$Pdbo->rNext()){
static $i=1;
$id=$array["id"];
$listNum=($Pdbo->currentPage-1)*$pageSize+$i;
$i++;
}
*/
?>