欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-04-02 16:02:52
核心代码: /* *pgsql类 */ class pgdb { public $pdo; public static $pdoinstan...

核心代码:

/*
*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;
  }
}