服务器之家

服务器之家 > 正文

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

时间:2021-02-24 16:09     来源/作者:php中文网

核心代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
*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;
  }
}
标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部