-
Notifications
You must be signed in to change notification settings - Fork 22
/
php-mysql.php
404 lines (351 loc) · 11.4 KB
/
php-mysql.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
/*
+=============================================================================================+
| PHP MYSQL ABSTRACTION CLASS |
+=============================================================================================+
| |
| Available Functions: |
| |
| * check |
| * query |
| * row |
| * results |
| * count |
| * affected_rows |
| |
+=============================================================================================+
| Author: Chris Ennis (https://github.com/dotrage) |
+=============================================================================================+
*/
define("DB_QUERY_REGEXP", "/(%d|%s|%%|%f|%b)/");
class DB {
private $connPool = array();
public function __construct($connection){
if (!empty($connection['host']) && !empty($connection['username']) && !empty($connection['password']) && !empty($connection['database'])){
$this->host = $connection['host'];
$this->user = $connection['username'];
$this->pass = $connection['password'];
$this->name = $connection['database'];
$this->port = null;
$this->socket = null;
if (!empty($connection['port'])){
$this->port = $connection['port'];
}
if (!empty($connection['socket'])){
$this->socket = $connection['socket'];
}
}
else if (!empty($connection) && is_array($connection)){
foreach ($connection as $c){
if (!empty($c['host']) && !empty($c['username']) && !empty($c['password']) && !empty($c['database']) && !empty($c['alias'])){
if (empty($c['port'])){
$c['port'] = null;
}
if (empty($c['socket'])){
$c['socket'] = null;
}
if (!empty($c['mode']) && ($c['mode'] == "r" || $c['mode'] == "w" || $c['mode'] == "rw")){
$mode = $c['mode'];
unset($c['mode']);
$this->connections[$mode][$c['alias']] = $c;
}
else{
unset($c['mode']);
$this->connections['rw'][$c['alias']] = $c;
}
}
}
}
}
//Confirm the server is accessible
public function check($connection=null){
$port = $socket = null;
if (!empty($connection)){
if (!empty($this->connections['r'][$connection]['host']) && !empty($this->connections['r'][$connection]['username']) && !empty($this->connections['r'][$connection]['password']) && !empty($this->connections['r'][$connection]['database'])){
if (!empty($this->connections['r'][$connection]['port'])){
$port = $this->connections['r'][$connection]['port'];
}
if (!empty($this->connections['r'][$connection]['socket'])){
$socket = $this->connections['r'][$connection]['socket'];
}
$dbc = @mysqli_connect($this->connections['r'][$connection]['host'],$this->connections['r'][$connection]['username'],$this->connections['r'][$connection]['password'],$this->connections['r'][$connection]['database'],$port,$socket);
if (mysqli_connect_errno()){
return false;
}
}
else if (!empty($this->connections['w'][$connection]['host']) && !empty($this->connections['w'][$connection]['username']) && !empty($this->connections['w'][$connection]['password']) && !empty($this->connections['w'][$connection]['database'])){
if (!empty($this->connections['w'][$connection]['port'])){
$port = $this->connections['w'][$connection]['port'];
}
if (!empty($this->connections['w'][$connection]['socket'])){
$socket = $this->connections['w'][$connection]['socket'];
}
$dbc = @mysqli_connect($this->connections['w'][$connection]['host'],$this->connections['w'][$connection]['username'],$this->connections['w'][$connection]['password'],$this->connections['w'][$connection]['database'],$port,$socket);
if (mysqli_connect_errno()){
return false;
}
}
else if (!empty($this->connections['rw'][$connection]['host']) && !empty($this->connections['rw'][$connection]['username']) && !empty($this->connections['rw'][$connection]['password']) && !empty($this->connections['rw'][$connection]['database'])){
if (!empty($this->connections['rw'][$connection]['port'])){
$port = $this->connections['rw'][$connection]['port'];
}
if (!empty($this->connections['rw'][$connection]['socket'])){
$socket = $this->connections['rw'][$connection]['socket'];
}
$dbc = @mysqli_connect($this->connections['rw'][$connection]['host'],$this->connections['rw'][$connection]['username'],$this->connections['rw'][$connection]['password'],$this->connections['rw'][$connection]['database'],$port,$socket);
if (mysqli_connect_errno()){
return false;
}
}
}
else{
if (!empty($this->host) && !empty($this->name) && !empty($this->user) && !empty($this->pass)){
$dbc = @mysqli_connect($this->host,$this->user,$this->pass,$this->name,$port,$socket);
if (mysqli_connect_errno()){
return false;
}
}
else if (!empty($this->connections)){
$output = array();
foreach ($this->connections as $conns){
foreach ($conns as $k=>$v){
if (!empty($v['host']) && !empty($v['username']) && !empty($v['password']) && !empty($v['database'])){
$port = $socket = null;
if (!empty($v['port'])){
$port = $v['port'];
}
if (!empty($v['socket'])){
$socket = $v['socket'];
}
$dbc = @mysqli_connect($v['host'],$v['username'],$v['password'],$v['database'],$port,$socket);
if (mysqli_connect_errno()){
$output[$k] = 0;
}
else{
$output[$k] = 1;
}
}
else{
$output[$k] = 0;
}
}
}
return $output;
}
else{
return false;
}
}
return true;
}
// Opens a connection to the database and sets up mysql_query object
public function query($sql){
$mode = "r";
$args = func_get_args();
array_shift($args);
if (isset($args[0]) and is_array($args[0])){
if (!empty($args[1]) && count($args)==2){
$mode = $args[1];
}
$args = $args[0];
}
$result = $this->run_query($sql,$args);
return $result;
}
private function run_query($sql,$args=array(),$mode="r"){
if (!empty($sql) && !is_array($sql)){
$sql = array("query" => $sql);
}
if (is_string($args)){
$args = func_get_args();
array_shift($args);
}
if (!empty($sql['show_error'])){
$show_error = $sql['show_error'];
}
else{
$show_error = TRUE;
}
if (!empty($this->host) && !empty($this->name) && !empty($this->user) && !empty($this->pass)){
$connection = array(
"host" => $this->host,
"username" => $this->user,
"password" => $this->pass,
"database" => $this->name,
"port" => $this->port,
"socket" => $this->socket
);
}
else if (!empty($this->connections)){
$arr1 = $arr2 = array();
if ($mode == "r"){
if (!empty($this->connections['r'])){
$arr1 = $this->connections['r'];
}
if (!empty($this->connections['rw'])){
$arr2 = $this->connections['rw'];
}
$arr3 = array_merge($arr1,$arr2);
}
else if ($mode == "w"){
if (!empty($this->connections['w'])){
$arr1 = $this->connections['w'];
}
if (!empty($this->connections['rw'])){
$arr2 = $this->connections['rw'];
}
$arr3 = array_merge($arr1,$arr2);
}
if (!empty($arr3)){
if (count($arr3)==1){
$connection = $arr3[0];
}
else{
$random = rand(0,count($arr3)-1);
$connection = $arr[$random];
}
}
}
if (empty($connection)){
printf("Invalid Database Connection");
exit;
}
if ($this->connPool[$connection['database']]) {
$conn = $this->connPool[$connection['database']];
}
else {
$conn = @mysqli_connect($connection['host'],$connection['username'],$connection['password'],$connection['database'],$connection['port'],$connection['socket']);
$this->connPool[$connection['database']] = $conn;
}
$this->conn = $conn;
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit;
}
$this->string_sanitize($args, TRUE);
$query = preg_replace_callback(DB_QUERY_REGEXP, array(&$this, 'string_sanitize'), $sql['query']);
mysqli_query($conn,"SET NAMES 'utf8'");
$result = mysqli_query($conn, $query);
if (!$result){
if ($show_error){
echo "Error querying database. : $query<br/>";
}
}
else{
return $result;
}
}
// Executes query and filters results into a single array or object
public function row($sql){
if (!empty($sql['output'])){
if ($sql['output'] == "array" || $sql['output'] == "object"){
$type = $sql['output'];
}
else{
$type = "array";
}
}
else{
$type = "array";
}
$args = func_get_args();
array_shift($args);
if (isset($args[0]) and is_array($args[0])){
$args = $args[0];
}
$result = $this->run_query($sql,$args);
if ($result){
$row = mysqli_fetch_assoc($result);
if ($type == "array"){
return $row;
}
if ($type == "object"){
return (object) $row;
}
}
}
// Executes query and filters results into an array or object
public function results($sql){
if (!empty($sql['output'])){
if ($sql['output'] == "array" || $sql['output'] == "object"){
$type = $sql['output'];
}
else{
$type = "array";
}
}
else{
$type = "array";
}
$args = func_get_args();
array_shift($args);
if (isset($args[0]) and is_array($args[0])){
$args = $args[0];
}
$result = $this->run_query($sql,$args);
$return = array();
while ($row = mysqli_fetch_assoc($result)){
if ($type == "array"){
$return[] = $row;
}
if ($type == "object"){
$return[] = (object) $row;
}
}
if ($result){
if ($type == "array"){
return $return;
}
if ($type == "object"){
return (object) $return;
}
}
}
// Executes query and returns row count for result set
public function count($sql){
$args = func_get_args();
array_shift($args);
if (isset($args[0]) and is_array($args[0])){
$args = $args[0];
}
$result = $this->run_query($sql,$args);
if ($result){
$row = mysqli_num_rows($result);
return $row;
}
return 0;
}
// Executes query and returns affected row count for result set
public function affected_rows($sql){
$args = func_get_args();
array_shift($args);
if (isset($args[0]) and is_array($args[0])){
$args = $args[0];
}
$result = $this->run_query($sql,$args);
if ($result){
$row = mysqli_affected_rows();
return $row;
}
}
private function string_sanitize($match,$init=FALSE){
static $args = NULL;
if ($init){
$args = $match;
return;
}
switch ($match[1]){
case '%d':
return (int) array_shift($args); // We don't need db_escape_string as numbers are db-safe
case '%s':
return mysqli_real_escape_string($this->conn,array_shift($args));
case '%%':
return '%';
case '%f':
return (float) array_shift($args);
case '%b': // binary data
return mysqli_real_escape_string($this->conn,array_shift($args));
}
}
}
?>