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

5年以前  |  阅读数:811 次  |  编程语言: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分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例