-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDB.php
More file actions
151 lines (133 loc) · 4.56 KB
/
Copy pathDB.php
File metadata and controls
151 lines (133 loc) · 4.56 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Created by PhpStorm.
* User: ronlee
* Date: 2019/4/25
* Time: 16:16
*/
class DB {
private static $_db = false;
private static $_dbSource = false;
private static $_config=array('dsn'=>'mysql:host=localhost;dbname=drugfitness;charset=utf8','user'=>'root','passwd'=>'123456');
private function __construct() {
}
private function __clone() {
die('clone of this DB is not allowed');
}
public static function getInstance(){
if(!(self::$_db instanceof self)){
self::$_db = new self();
}
return self::$_db;
}
public function getConnect($origin=false){
if (!self::$_dbSource){
try{
self::$_dbSource = new PDO(self::$_config['dsn'],self::$_config['user'],self::$_config['passwd']);
self::$_dbSource->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
self::$_dbSource->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
}catch (PDOException $e){
die("数据库连接出错: ".$e->getMessage());
}
}
return $origin?self::$_dbSource:self::$_db;
}
public function insert($table, $data){
$value = 'NULL,';
$condition = 'id,';
foreach ($data as $key=>$v){
$condition.=$key.',';
$value.='\''.$v.'\',';
}
$condition = trim($condition,',');
$value = trim($value,',');
$sql = 'INSERT INTO '.$table.' ('.$condition.') VALUES ('.$value.')';
$res = self::$_dbSource->exec($sql);
return $res;
}
public function update($table, $data, $where){
$update = $this->parseUpdate($data);
$w = $this->parseWhere($where);
$sql = 'UPDATE '.$table.' SET '.$update.' WHERE '.$w;
$res = self::$_dbSource->exec($sql);
return $res;
}
public function select($table, $field ='*',$where, $limit=1){
$w = $this->parseWhere($where);
$Field = $this->parseField($field);
$sql = 'SELECT '.$Field.' FROM '.$table.' WHERE '.$w.' LIMIT '.$limit;
$res = self::$_dbSource->query($sql)->fetchAll();
return $res;
}
public function getCount($table, $where=array()){
if(count($where) != 0){
$w = $this->parseWhere($where);
$sql = 'SELECT COUNT(id) AS total FROM '.$table.' WHERE '.$w;
}else{
$sql = 'SELECT COUNT(id) AS total FROM '.$table;
}
$res = self::$_dbSource->query($sql)->fetchAll();
return intval($res[0]['total']);
}
public function has($table, $where=array()){
$w = $this->parseWhere($where);
$sql = 'SELECT * FROM '.$table.' WHERE '.$w;
$res = self::$_dbSource->query($sql)->fetchAll();
if($res){
return true;
}else{
return false;
}
}
public function delete($table, $where=array()){
$w = $this->parseWhere($where);
$sql = 'DELETE FROM '.$table.' WHERE '.$w;
$res = self::$_dbSource->exec($sql);
return $res;
}
public function error(){
return self::$_dbSource->errorInfo();
}
public function info(){
$output = array(
'server' => 'SERVER_INFO',
'driver' => 'DRIVER_NAME',
'client' => 'CLIENT_VERSION',
'version' => 'SERVER_VERSION',
'connection' => 'CONNECTION_STATUS'
);
foreach ($output as $key => $value) {
$output[$key] = self::$_dbSource->getAttribute(constant('PDO::ATTR_' . $value));
}
$output['dsn'] = self::$_config['dsn'];
return $output;
}
private function parseWhere($where=array()){
$w='';
foreach ($where as $k=>$value){
$w.=$k.'=\''.$value.'\' AND ';
}
return trim($w,' AND ');
}
private function parseField($field){
$res = NULL;
if(is_string($field) && $field == '*'){
$res = '*';
}elseif (is_array($field)){
$res = implode(',',$field);
}
return $res;
}
private function parseUpdate($data=array()){
$update = '';
foreach ($data as $key=>$v){
if(is_array($v)){
$update.=$key.'='.$key.implode('',$v).',';
}else{
$update.=$key.'=\''.$v.'\',';
}
}
$update = trim($update,',');
return $update;
}
}