forked from Code4SierraLeone/MembaO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_db.php
450 lines (394 loc) · 11.9 KB
/
class_db.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
<?php
/**
* Database Class
*
* @package Membao
* @author Alan Kawamara
* @copyright 2017
*/
if (!defined("_VALID_PHP"))
die('Direct access to this location is not allowed.');
class Database
{
private $server = "";
private $user = "";
private $pass = "";
private $database = "";
public $error = "";
public $errno = 0;
protected $affected_rows = 0;
protected $query_counter = 0;
protected $link_id = 0;
protected $query_id = 0;
protected $query_show;
/**
* Database::__construct()
*
* @param mixed $server
* @param mixed $user
* @param mixed $pass
* @param mixed $database
* @return
*/
function __construct($server, $user, $pass, $database)
{
$this->server = $server;
$this->user = $user;
$this->pass = $pass;
$this->database = $database;
}
/**
* Database::connect()
* Connect and select database using vars above
* @return
*/
public function connect()
{
$this->link_id = $this->connect_db($this->server, $this->user, $this->pass);
if (!$this->link_id)
$this->error("<div style='text-align:center'>"
. "<span style='padding: 5px; border: 1px solid #999; background-color:#EFEFEF;"
. "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
. "<b>Database Error:</b>Connection to Database " . $this->database . " Failed</span></div>");
if (!$this->select_db($this->database, $this->link_id))
$this->error("<div style='text-align:center'>"
. "<span style='padding: 5px; border: 1px solid #999; background-color: #EFEFEF;"
. "font-family: Verdana; font-size: 11px; margin-left:auto; margin-right:auto'>"
. "<b>Database Error:</b>mySQL database (" . $this->database . ")cannot be used</span></div>");
mysqli_set_charset($this->link_id, "utf8");
unset($this->password);
}
/**
* Database::connect_db()
*
* @param mixed $server
* @param mixed $user
* @param mixed $pass
* @return
*/
private function connect_db($server, $user, $pass)
{
return mysqli_connect($server, $user, $pass);
}
/**
* Database::select_db()
*
* @param mixed $database
* @param mixed $link_id
* @return
*/
private function select_db($database, $link_id)
{
return mysqli_select_db($link_id, $database);
}
/**
* Database::query()
* Executes SQL query to an open connection
* @param mixed $sql
* @return (query_id)
*/
public function query($sql)
{
if (trim($sql != "")) {
$this->query_counter++;
$this->query_show .= stripslashes($sql) . "<hr size='1' />";
$this->query_id = mysqli_query($this->link_id, $sql);
$this->last_query = $sql . '<br />';
}
if (!$this->query_id)
$this->error("mySQL Error on Query : " . $sql);
return $this->query_id;
}
/**
* Database::first()
* Fetches the first row only, frees resultset
* @param mixed $string
* @param bool $type
* @return array
*/
public function first($string, $type = false)
{
$query_id = $this->query($string);
$record = $this->fetch($query_id, $type);
$this->free($query_id);
return $record;
}
/**
* Database::fetch()
* Fetches and returns results one line at a time
* @param integer $query_id
* @param bool $type
* @return array
*/
public function fetch($query_id, $type = false)
{
if ($query_id)
$this->query_id = $query_id;
if (isset($this->query_id)) {
$record = ($type) ? mysqli_fetch_array($this->query_id) : mysqli_fetch_object($this->query_id);
} else
$this->error("Invalid query_id: <b>" . $this->query_id . "</b>. Records could not be fetched.");
return $record;
}
/**
* Database::fetch_all()
* Returns all the results
* @param mixed $sql
* @param bool $type
* @return assoc array
*/
public function fetch_all($sql, $type = false)
{
$query_id = $this->query($sql);
$record = array();
while ($row = $this->fetch($query_id, $type)) :
$record[] = $row;
endwhile;
$this->free($query_id);
return $record;
}
/**
* Database::free()
* Frees the resultset
* @param integer $query_id
* @return query_id
*/
private function free($query_id)
{
if ($query_id)
$this->query_id = $query_id;
return mysqli_free_result($this->query_id);
}
/**
* Database::insert()
* Insert query with an array
* @param mixed $table
* @param mixed $data
* @return id of inserted record, false if error
*/
public function insert($table = null, $data)
{
if ($table === null or empty($data) or !is_array($data)) {
$this->error("Invalid array for table: <b>".$table."</b>.");
return false;
}
$q = "INSERT INTO `" . $table . "` ";
$v = '';
$k = '';
foreach ($data as $key => $val) :
$k .= "`$key`, ";
if (strtolower($val) == 'null')
$v .= "NULL, ";
elseif (strtolower($val) == 'now()')
$v .= "NOW(), ";
else
$v .= "'" . $this->escape($val) . "', ";
endforeach;
$q .= "(" . rtrim($k, ', ') . ") VALUES (" . rtrim($v, ', ') . ");";
if ($this->query($q)) {
return $this->insertid();
} else
return false;
}
/**
* Database::update()
* Update query with an array
* @param mixed $table
* @param mixed $data
* @param string $where
* @return query_id
*/
public function update($table = null, $data, $where = '1')
{
if ($table === null or empty($data) or !is_array($data)) {
$this->error("Invalid array for table: <b>" . $table . "</b>.");
return false;
}
$q = "UPDATE `" . $table . "` SET ";
foreach ($data as $key => $val) :
if (strtolower($val) == 'null')
$q .= "`$key` = NULL, ";
elseif (strtolower($val) == 'now()')
$q .= "`$key` = NOW(), ";
elseif (strtolower($val) == 'default()')
$q .= "`$key` = DEFAULT($val), ";
elseif(preg_match("/^inc\((\-?[\d\.]+)\)$/i",$val,$m))
$q.= "`$key` = `$key` + $m[1], ";
else
$q .= "`$key`='" . $this->escape($val) . "', ";
endforeach;
$q = rtrim($q, ', ') . ' WHERE ' . $where . ';';
return $this->query($q);
}
/**
* Database::delete()
* Delete records
* @param mixed $table
* @param string $where
* @return
*/
public function delete($table, $where = '')
{
$q = !$where ? 'DELETE FROM ' . $table : 'DELETE FROM ' . $table . ' WHERE ' . $where;
return $this->query($q);
}
/**
* Database::insert_id()
* Returns last inserted ID
* @param integer $query_id
* @return
*/
public function insertid()
{
return mysqli_insert_id($this->link_id);
}
/**
* Database::affected()
* Returns the number of affected rows
* @param integer $query_id
* @return
*/
public function affected() {
return mysqli_affected_rows($this->link_id);
}
/**
* Database::numrows()
*
* @param integer $query_id
* @return
*/
public function numrows($query_id)
{
if ($query_id)
$this->query_id = $query_id;
$this->num_rows = mysqli_num_rows($this->query_id);
return $this->num_rows;
}
/**
* Database::fetchrow()
* Fetches one row of data
* @param integer $query_id
* @return fetched row
*/
public function fetchrow($query_id)
{
if ($query_id)
$this->query_id = $query_id;
$this->fetch_row = mysqli_fetch_row($this->query_id);
return $this->fetch_row;
}
/**
* Database::numfields()
*
* @param integer $query_id
* @return
*/
public function numfields($query_id)
{
if ($query_id)
$this->query_id = $query_id;
$this->num_fields = mysqli_num_fields($this->query_id);
return $this->num_fields;
}
/**
* Database::show()
*
* @return
*/
public function show()
{
return "<br /><br /><b> Debug Mode - All Queries :</b><hr size='1' /> " . $this->query_show . "<br />";
}
/**
* Database::pre()
*
* @return
*/
public function pre($arr)
{
print '<pre>' . @print_r($arr, true) . '</pre>';
}
/**
* Database::escape()
* @param mixed $string
* @return
*/
public function escape($string)
{
if (is_array($string)) {
foreach ($string as $key => $value) :
$string[$key] = $this->escape_($value);
endforeach;
} else
$string = $this->escape_($string);
return $string;
}
/**
* Database::escape_()
*
* @param mixed $string
* @return Database::quote()
*/
private function escape_($string)
{
return mysqli_real_escape_string($this->link_id, $string);
}
/**
* Database::getDB()
*
* @return
*/
public function getDB()
{
return $this->database;
}
/**
* Database::getServer()
*
* @return
*/
public function getServer()
{
return $this->server;
}
/**
* Database::getLink()
*
* @return
*/
public function getLink()
{
return $this->link_id;
}
/**
* Database::error()
* Output error message
* @param mixed $msg
* @return
*/
public function error($msg = '')
{
if (!is_resource($this->link_id)) {
$this->error_desc = mysqli_error($this->link_id);
$this->error_no = mysqli_errno($this->link_id);
} else {
$this->error_desc = mysqli_error($this->link_id);
$this->error_no = mysqli_errno($this->link_id);
}
$the_error = "<div style=\"background-color:#FFF; border: 3px solid #999; padding:10px\">";
$the_error .= "<b>mySQL WARNING!</b><br />";
$the_error .= "DB Error: $msg <br /> More Information: <br />";
$the_error .= "<ul>";
$the_error .= "<li> Mysql Error : " . $this->error_no . "</li>";
$the_error .= "<li> Mysql Error no # : " . $this->error_desc . "</li>";
$the_error .= "<li> Date : " . date("F j, Y, g:i a") . "</li>";
$the_error .= "<li> Referer: " . isset($_SERVER['HTTP_REFERER']) . "</li>";
$the_error .= "<li> Script: " . $_SERVER['REQUEST_URI'] . "</li>";
$the_error .= '</ul>';
$the_error .= '</div>';
if (DEBUG)
echo $the_error;
die();
}
}
?>