我写的一个PHP连接 数据库的类(PostgreSQL版)

时间:2021-12-23 10:17:52
<?php
/**
* Author: 景银振    
* Description: A class for PostgreSQL connection of PHP
* DateTime: 2009-05-26 16:33:06
* Update:     2009-06-16 13:03:45
* Version: 1.0.2
*     你可以随便使用 修改 剪切 这个类 ,但必须 在 代码的开头 保留 作者以及版本信息,否则将追究责任
*/ 

    
require_once("Util/exceptions.php");
    
class model{
  private $conn;
  function __construct($pConnect=false){
    $host='localhost';
    $port='5432';
    $user='postgres';
    $password='test';
    $dbname='qeshow_test';
    $this->conn=pg_connect("host=$host dbname=$dbname user=$user password=$password port=$port")or die("数据库连接失败"); # 默认为永久连接 pg_pconnect()
                if($pConnect){
        $this->conn=pg_pconnect("host=$host dbname=$dbname user=$user password=$password port=$port");    
    }                        
                    
  }
  function __desrtruct(){
        pg_close($this->conn); // 对象销毁的时候关闭连接
  }
  function Query($sql){
         # Discription:根据查询语句,返回一个二维数组
         # @ param $sql: SQL语句
    @$result=pg_query($sql);
    try{     ## 如果SQL语句错误的话$result为布尔false,抛出SQL语句错误的异常
      if(!$result){
        throw new SQLErrorException("SQL语句有误,[".$sql."]");
      }
    }catch (SQLErrorException $see){
      $see->showMsg();
    }
    $arr=array();
     while($row=pg_fetch_assoc($result)){ #利用循环将查询结果填充到二维数组
             array_push($arr,$row);        
     }
     return $arr;
  }
        public function getPgIndex($rcdNum,$num,$page,$url,$param){
                $pgStr='';
                if($rcdNum%$num==0){
                     $pgNum=$rcdNum/$num;
                }else{
                     $pgNum=floor($rcdNum/$num);
                }
                $ext='';
                foreach($param as $key=>$value){
                     $ext.=($key."=".$value."&");
                }
                $url.=('?'.$ext);
        
                $pgStr.=("<a href='".$url."page=1'><Font face=webdings>9</font></a>  ");
                if($page-2>=0){
            if($page+10>$pgNum){
             for($i=$pgNum-9;$i<=$pgNum;++$i){
                                if($i==$page){
                                 $pgStr.=("<a href='".$url."page=".$i."'><b>".$i."</b></a>  ");
                         }else{
                                 $pgStr.=("<a href='".$url."page=".$i."'>".$i."</a>  ");
                         }        
        }
      }else{
                                for($i=$page-2;$i<$page+8;++$i){
                                if($i==$page){
                                 $pgStr.=("<a href='".$url."page=".$i."'><b>".$i."</b></a>  ");
                         }else{
                                 $pgStr.=("<a href='".$url."page=".$i."'>".$i."</a>  ");
                         }
                         }        
      }

                }else{
                     for($i=1;$i<=10;++$i)     {
                     if($i==$page){
                        $pgStr.=("<a href='".$url."page=".$i."'><b>".$i."</b></a>  ");
                }else{
                        $pgStr.=("<a href='".$url."page=".$i."'>".$i."</a>  ");
                }
                }
                }
                if($pgNum>1){
                     $pgStr.=("<a href='".$url."page=".$pgNum."'>...".$pgNum."<Font face=webdings>:</font></a>  ");
                }
                return $pgStr;
         }
        public function paggingQuery($sql,$page=1,$num=10){
        $page--;
        $query=$sql." limit ".$num." offset ".($num*$page);
        $re=$this->Query($query);
        return $re;
  }
  public function affectRowQuery($sql){    
         # 返回查询影响行数
    # @ param $sql SQL语句
                //echo $sql;
    $re=pg_query($this->conn,$sql);
    //var_dump($re);
                try{
                  if($re===false){
                    throw new SQLErrorException("SQL语句有误,[".$sql.']');
                  }
                }catch (SQLErrorException $see){
                  $see->showMsg();
                }
    return pg_affected_rows($re);
  }
  function insert($table,$arr,$keys=''){
                # Description: 从数组中向数据表添加数据,成功返回true,失败返回false
    # @ param $table 要操作的数据表
    # @ param $arr 数组,必须以字段名为下标
    # @ param $keys    数据表主键数组,推荐用以逗号分开的字符串
    if(empty($table) || empty($arr)){
         return $this->paramError;    
    }
    ### 查询要插入的记录是否已存在
    
    if($keys){
            $keyArr=array();
            $ext="select * from ".$table." where ";    
            $where='';
            if(is_string($keys)){
              $keyArr=explode(',',$keys);
            }
            if(is_array($keys)){
              $keyArr=$keys;
            }
                 foreach ($keyArr as $key){
              $where.=(" $key='".$arr[$key]."' and");
            }
            $where=substr($where,0,strlen($where)-4);
            //echo    $arr['usr'];
            $ext.=$where;
            //echo $ext;
            $re=$this->Query($ext);
      if($re){
            return "exists";
      }
            //echo "<br>Affected".$row;;
                            
    }
                
    ###END查询是否存在####
    
    $query=" INSERT INTO    ".$table; ## 拼接SQL语句
    $cols='';
    $values='';                
    foreach ( $arr as $col=>$value){    
      $cols.=($col.',');             // 获取sql语句要插入的字段
      $values.=("'".$value."',"); # 获取sql语句要插入的字段值            
    }
    $cols='('.substr($cols,0,strlen($cols)-1).')';
    $values='('.substr($values,0,strlen($values)-1).')';
    $query.=$cols.' VALUES '.$values;    # 生成查询语句
    //echo $query;
    $num=$this->affectRowQuery($query);
    //echo $num;
    if($num==1){
      return "succeed";
    }else{
      return "fail";
    }
  }
    
  function update($table,$arr,$where=''){
                # Discript: 根据数组修改数据表的数据,如果执行成功,返回影响行数,否则返回false
    # @ param $table 要操作的数据表
    # @ param $arr 数组,必须以字段名为下标
    # @ $where UPDATE语句中的where子句

    $query="UPDATE ".$table." set ";
    $set='';
    foreach($arr as $col=>$value){
            $set.=(' '.$col." = '".$value."', ");
    }
    $set=substr($set,0,strlen($set)-2); # 去掉最后的逗号
    $query.=($set ." ".$where);
    //echo $query;
    $num=$this->affectRowQuery($query);
    var_dump($num);
    if($num<=0){
            return "fail";
    }else{
            return "succeed";
    }
  }
  public function begin(){
         $this->affectRowQuery("begin;");    // Begin transAction
    $this->affectRowQuery("savepoint sp"); // Create savepoint
  }
        public function commit(){
    $this->affectRowQuery("commit");
  }
  public function rollback(){
         $this->affectRowQuery("rollback to savepoint sp");
  }    
}

?>
请把附件的扩展名改回 .php

本文出自 “小景的博客,搬迁至 ht..” 博客,转载请与作者联系!