简单的pgsql pdo php操作类实现代码

yipeiwu_com5年前PHP代码库

核心代码:

/*
*pgsql类
*/
class pgdb {


 public $pdo;
 public static $PDOInstance;
 public $config;
 public $data;
 public $filed = '*';
 public $table;
 public $limit;
 public $order;
 public $where;
 public $left;


 const LOGIN = 7;
 const USER = 1;
 const GROUP = 2;
 const USERGROUP = 3;
 const LOG = 6;
 const WARING = 1;
 const ERROR = 2;
 const INFO = 0;


 public function __construct() {
  if (!self::$PDOInstance) {
    $this->config = json_decode(file_get_contents("./config/db.json"), true);


    $config = $this->config;
    $host = $config["data_base"]["db_host"];
    $dbname = $config["data_base"]["db_name"];
    $port = $config["data_base"]["db_port"];
    $username = $config["data_base"]["db_user"];
    $password = $config["data_base"]["db_pwd"];


    if ($config["data_base"]["db_host"] != 'localhost') {
      $hosturl = "host=$host;";
    }
    try
    {
      self::$PDOInstance = new PDO("pgsql:"
        . $hosturl
        . "port=$port;"
        . "dbname=$dbname;"
        , $username
        , $password
        , array(
          PDO::ATTR_PERSISTENT => true,
        )
      );
    } catch (Exception $ex) {
      header("Content-type: text/html; charset=utf-8");
      $error = "数据库初始化失败,已强制断开链接。<br />抓取到的异常栈如下:<br /><pre>" . print_r($ex, true) . "</pre>";
      die($error);
    }
    try {
      self::$PDOInstance->query("SET client_encoding='UTF-8';");
      self::$PDOInstance->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
      self::$PDOInstance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } catch (Exception $exc) {
     $this->pdo=NULL;
     $doc = <<<DOC
     <!DOCTYPE html>
     <html>
      <head>
        <meta charset="UTF-8">
        <script src="layer/jquery-1.11.1.min.js"></script>
        <script src="layer/layer.js"></script>
      <head>
     <body>
DOC;


    print $doc;
    $info=L('服务器变更请刷新');
    print("<script>layer.msg('".$info."', {icon: 2,time: 30000},function(){location.reload();});</script>");
    print('</body></html>');
    exit();
    }
   }
  
   $this->pdo = self::$PDOInstance;
  }
}

相关文章

php 备份数据库代码(生成word,excel,json,xml,sql)

单表备份代码:复制代码 代码如下:<?php    class Db    {    &n...

PHP IPV6正则表达式验证代码

复制代码 代码如下:<?php // literally from the ABNF in rfc3986 (thanks to 'WCP') function validateI...

MyEclipse常用配置图文教程

MyEclipse常用配置图文教程

  MyEclipse有很多功能,但是我们经常用到的功能其实并不是特别多,在这里将一些有用的小技巧记录下来,作为备忘录,同时也希望能够对他人有些许帮助吧。 一 工作组(working s...

用PHP将Unicode 转化为UTF-8的实现方法(推荐)

实例如下: function unescape($str) { $str = rawurldecode($str); preg_match_all("/(?:%u.{...

php使用PDO执行SQL语句的方法分析

本文实例讲述了php使用PDO执行SQL语句的方法。分享给大家供大家参考,具体如下: exec()方法 exec()方法返回执行后受影响行数,语法如下: int PDO::exec(st...