-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgsql.php
More file actions
96 lines (89 loc) · 2.36 KB
/
pgsql.php
File metadata and controls
96 lines (89 loc) · 2.36 KB
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
91
92
93
94
95
96
<?php
/* (C)2006-2021 FoundPHP Framework.
* name: Database Object
* weburl: http://www.FoundPHP.com
* mail: master@FoundPHP.com
* author: 孟大川
* version: v3.201212
* start: 2006-05-24
* update: 2020-12-12
* payment: Free 免费
* This is not a freeware, use is subject to license terms.
* 此软件为授权使用软件,请参考软件协议。
* http://www.foundphp.com/?m=agreement
*/
Class Dirver_pgsql{
public $nums = 0;
public $Lists = '';
public $insert_id = 0;
//连接数据库
function DBLink($dba=''){
$dba['dbport'] = @$dba['dbport']?$dba['dbport']:5432;
$this->LinkID = pg_connect("host=".$dba['dbhost']." port=".$dba['dbport']." dbname=".$dba['dbname']." user=".$dba['dbuser']." password=".$dba['dbpass']);
if (!$this->LinkID){ return 2; }
if (@$dba['charset']){
$this->charset = $dba['charset'];
}
$this->set_names= 0;
return $this->LinkID;
}
//查询语句
function query($query,$limit='') {
$query = trim($query);
//检测如果有限制数据集则处理
if($limit>0){
$query = $query.' LIMIT '.$limit.' OFFSET 0;';
}
//时间处理
if (stristr($query, "now()") !== FALSE){
$query = preg_replace("/(.*)\'now\(\)\'(.*)/is","\\1now()\\2",$query);
}
if (stristr($query,'insert into')){
$querys = substr($query,-1);
if ($querys==';'){
$query = substr($query,0,strlen($query)-1);
}
$query .= ' RETURNING id;';
$pgq = pg_query($this->LinkID,$query);
$pg_ins = $this->fetch_array($pgq);
$this->insert_id = $pg_ins[0];
}else{
$pgq = pg_query($this->LinkID,$query);
}
return array('query'=>$pgq,'sql'=>$query);
}
//返回数组资料
function fetch_array($query) {
return @pg_fetch_array($query);
}
//取得返回列的数目
function num_rows($query){
return @pg_num_rows($query);
}
//返回最后一次使用 INSERT 指令的 ID
function insert_id(){
return $this->insert_id;
}
//创建数据库
function create_db($dbname){
pg_query($this->LinkID,"CREATE DATABASE ".$dbname);
}
//列出数据库
function show_db(){
$query = pg_query($this->LinkID,"SHOW DATABASES ");
while($r = $this->fetch_array($query)){
$result[] = $r['Database'];
}
return $result;
}
//关闭当前数据库连接
function close(){
return pg_close($this->LinkID);
}
//检测版本
function version(){
$v = pg_version($this->LinkID);
return $v['client'];
}
}
?>