|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* PHP-PDO-MySQL-Class |
|
5
|
|
|
* https://github.com/lincanbin/PHP-PDO-MySQL-Class |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright 2015 Canbin Lin ([email protected]) |
|
8
|
|
|
* http://www.94cb.com/ |
|
9
|
|
|
* |
|
10
|
|
|
* Licensed under the Apache License, Version 2.0: |
|
11
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
12
|
|
|
* |
|
13
|
|
|
* A PHP MySQL PDO class similar to the the Python MySQLdb. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace responsible\core\connect; |
|
17
|
|
|
|
|
18
|
|
|
use PDO; |
|
19
|
|
|
|
|
20
|
|
|
class DB |
|
21
|
|
|
{ |
|
22
|
|
|
private $Host; |
|
23
|
|
|
private $DBName; |
|
24
|
|
|
private $DBUser; |
|
25
|
|
|
private $DBPassword; |
|
26
|
|
|
private $DBPort; |
|
27
|
|
|
private $pdo; |
|
28
|
|
|
private $sQuery; |
|
29
|
|
|
private $bConnected = false; |
|
30
|
|
|
private $parameters; |
|
31
|
|
|
public $rowCount = 0; |
|
32
|
|
|
public $columnCount = 0; |
|
33
|
|
|
public $querycount = 0; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct($Host, $DBName, $DBUser, $DBPassword, $DBPort = 3306) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->Host = $Host; |
|
38
|
|
|
$this->DBName = $DBName; |
|
39
|
|
|
$this->DBUser = $DBUser; |
|
40
|
|
|
$this->DBPassword = $DBPassword; |
|
41
|
|
|
$this->DBPort = $DBPort; |
|
42
|
|
|
$this->Connect(); |
|
43
|
|
|
$this->parameters = array(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function Connect() |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
$this->pdo = new PDO( |
|
50
|
|
|
'mysql:dbname=' . $this->DBName . ';host=' . $this->Host . ';port=' . $this->DBPort . ';charset=utf8', |
|
51
|
|
|
$this->DBUser, |
|
52
|
|
|
$this->DBPassword, |
|
53
|
|
|
array( |
|
54
|
|
|
//For PHP 5.3.6 or lower |
|
55
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", |
|
56
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
|
57
|
|
|
|
|
58
|
|
|
//长连接 |
|
59
|
|
|
//PDO::ATTR_PERSISTENT => true, |
|
60
|
|
|
|
|
61
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
|
62
|
|
|
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->bConnected = true; |
|
67
|
|
|
} catch (PDOException $e) { |
|
|
|
|
|
|
68
|
|
|
echo $this->ExceptionLog($e->getMessage()); |
|
69
|
|
|
die(); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function CloseConnection() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->pdo = null; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function Init($query, $parameters = "") |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$this->bConnected) { |
|
81
|
|
|
$this->Connect(); |
|
82
|
|
|
} |
|
83
|
|
|
try { |
|
84
|
|
|
$this->parameters = $parameters; |
|
85
|
|
|
$this->sQuery = $this->pdo->prepare($this->BuildParams($query, $this->parameters)); |
|
86
|
|
|
|
|
87
|
|
|
if (!empty($this->parameters)) { |
|
88
|
|
|
if (array_key_exists(0, $parameters)) { |
|
89
|
|
|
$parametersType = true; |
|
90
|
|
|
array_unshift($this->parameters, ""); |
|
91
|
|
|
unset($this->parameters[0]); |
|
92
|
|
|
} else { |
|
93
|
|
|
$parametersType = false; |
|
94
|
|
|
} |
|
95
|
|
|
foreach ($this->parameters as $column => $value) { |
|
96
|
|
|
$this->sQuery->bindParam($parametersType ? intval($column) : ":" . $column, $this->parameters[$column]); //It would be query after loop end(before 'sQuery->execute()').It is wrong to use $value. |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$this->sQuery->execute(); |
|
101
|
|
|
$this->querycount++; |
|
102
|
|
|
} catch (PDOException $e) { |
|
103
|
|
|
echo $this->ExceptionLog($e->getMessage(), $this->BuildParams($query)); |
|
104
|
|
|
die(); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->parameters = array(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
private function BuildParams($query, $params = array()) |
|
111
|
|
|
{ |
|
112
|
|
|
if (!empty($params)) { |
|
113
|
|
|
$array_parameter_found = false; |
|
114
|
|
|
foreach ($params as $parameter_key => $parameter) { |
|
115
|
|
|
if (is_array($parameter)) { |
|
116
|
|
|
$array_parameter_found = true; |
|
117
|
|
|
$in = ""; |
|
118
|
|
|
foreach ($parameter as $key => $value) { |
|
119
|
|
|
$name_placeholder = $parameter_key . "_" . $key; |
|
120
|
|
|
// concatenates params as named placeholders |
|
121
|
|
|
$in .= ":" . $name_placeholder . ", "; |
|
122
|
|
|
// adds each single parameter to $params |
|
123
|
|
|
$params[$name_placeholder] = $value; |
|
124
|
|
|
} |
|
125
|
|
|
$in = rtrim($in, ", "); |
|
126
|
|
|
$query = preg_replace("/:" . $parameter_key . "/", $in, $query); |
|
127
|
|
|
// removes array form $params |
|
128
|
|
|
unset($params[$parameter_key]); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// updates $this->params if $params and $query have changed |
|
133
|
|
|
if ($array_parameter_found) { |
|
134
|
|
|
$this->parameters = $params; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
return $query; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC) |
|
141
|
|
|
{ |
|
142
|
|
|
$query = trim(preg_replace('/\s+/', ' ', $query)); |
|
143
|
|
|
$rawStatement = explode(" ", $query); |
|
144
|
|
|
$this->Init($query, $params); |
|
145
|
|
|
$statement = strtolower($rawStatement[0]); |
|
146
|
|
|
if ($statement === 'select' || $statement === 'show') { |
|
147
|
|
|
return $this->sQuery->fetchAll($fetchmode); |
|
148
|
|
|
} elseif ($statement === 'insert' || $statement === 'update' || $statement === 'delete') { |
|
149
|
|
|
return $this->sQuery->rowCount(); |
|
150
|
|
|
} else { |
|
151
|
|
|
return null; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function lastInsertId() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->pdo->lastInsertId(); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function column($query, $params = null) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->Init($query, $params); |
|
163
|
|
|
$resultColumn = $this->sQuery->fetchAll(PDO::FETCH_COLUMN); |
|
164
|
|
|
$this->rowCount = $this->sQuery->rowCount(); |
|
165
|
|
|
$this->columnCount = $this->sQuery->columnCount(); |
|
166
|
|
|
$this->sQuery->closeCursor(); |
|
167
|
|
|
return $resultColumn; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function row($query, $params = null, $fetchmode = PDO::FETCH_ASSOC) |
|
171
|
|
|
{ |
|
172
|
|
|
$this->Init($query, $params); |
|
173
|
|
|
$resultRow = $this->sQuery->fetch($fetchmode); |
|
174
|
|
|
$this->rowCount = $this->sQuery->rowCount(); |
|
175
|
|
|
$this->columnCount = $this->sQuery->columnCount(); |
|
176
|
|
|
$this->sQuery->closeCursor(); |
|
177
|
|
|
return $resultRow; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function single($query, $params = null) |
|
181
|
|
|
{ |
|
182
|
|
|
$this->Init($query, $params); |
|
183
|
|
|
return $this->sQuery->fetchColumn(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private function ExceptionLog($message, $sql = "") |
|
187
|
|
|
{ |
|
188
|
|
|
$exception = 'Unhandled Exception. <br />'; |
|
189
|
|
|
$exception .= $message; |
|
190
|
|
|
$exception .= "<br /> You can find the error back in the log."; |
|
191
|
|
|
|
|
192
|
|
|
if (!empty($sql)) { |
|
193
|
|
|
$message .= "\r\nRaw SQL : " . $sql; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
//Prevent search engines to crawl |
|
197
|
|
|
header("HTTP/1.1 500 Internal Server Error"); |
|
198
|
|
|
header("Status: 500 Internal Server Error"); |
|
199
|
|
|
return $exception; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function beginTransaction() |
|
203
|
|
|
{ |
|
204
|
|
|
$this->pdo->beginTransaction(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function commit() |
|
208
|
|
|
{ |
|
209
|
|
|
$this->pdo->commit(); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function rollBack() |
|
213
|
|
|
{ |
|
214
|
|
|
$this->pdo->rollBack(); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|