1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MOOM; |
4
|
|
|
use PDO; |
5
|
|
|
use PDOException; |
6
|
|
|
class DB |
7
|
|
|
{ |
8
|
|
|
//Database host |
9
|
|
|
public $host = "localhost"; |
10
|
|
|
//Database username |
11
|
|
|
public $username = "root"; |
12
|
|
|
//Database password |
13
|
|
|
public $password = ""; |
14
|
|
|
//Database name |
15
|
|
|
public $db = 'myblog'; |
16
|
|
|
// Hold the class instance. |
17
|
|
|
private static $instance; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DB constructor. |
21
|
|
|
*/ |
22
|
|
|
protected function __construct() |
23
|
|
|
{ |
24
|
|
|
try { |
25
|
|
|
self::$instance = new PDO("mysql:host=$this->host;dbname=$this->db", $this->username, $this->password); |
26
|
|
|
// set the PDO error mode to exception |
27
|
|
|
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
28
|
|
|
} catch |
29
|
|
|
(PDOException $e) { |
30
|
|
|
return 'fail'; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create new Instance of DB Class |
36
|
|
|
* @return PDO |
37
|
|
|
*/ |
38
|
|
|
public static function getInstance() |
39
|
|
|
{ |
40
|
|
|
if (self::$instance == null) { |
41
|
|
|
new DB(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return self::$instance; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $tbl_name |
49
|
|
|
* @param $params |
50
|
|
|
* @return \PDOStatement |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
private static function select($tbl_name, $params) |
54
|
|
|
{ |
55
|
|
|
$string = "SELECT * FROM $tbl_name"; |
56
|
|
|
if (isset($params['where'])) { |
57
|
|
|
$string .= " WHERE {$params['where']['column']} {$params['where']['operator']}" . ":" . "{$params['where']['column']}"; |
58
|
|
|
} |
59
|
|
|
if (isset($params['order'])) { |
60
|
|
|
$string .= " order by {$params['order']['key']} {$params['order']['type']}"; |
61
|
|
|
} |
62
|
|
|
if (isset($params['limit'])) { |
63
|
|
|
$string .= " LIMIT {$params['limit']}"; |
64
|
|
|
} |
65
|
|
|
$instance = self::$instance; |
66
|
|
|
$stmt = $instance->prepare($string); |
67
|
|
|
if (isset($params['where']) && !empty($params['where'])) { |
68
|
|
|
$key = ":" . $params['where']['column']; |
69
|
|
|
$value = $params['where']['value']; |
70
|
|
|
$stmt->bindValue($key, $value); |
71
|
|
|
} |
72
|
|
|
$stmt->execute(); |
73
|
|
|
|
74
|
|
|
$stmt->setFetchMode(PDO::FETCH_OBJ); |
75
|
|
|
return $stmt; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $tbl_name |
80
|
|
|
* @param $params |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public static function fetch($tbl_name, $params) |
84
|
|
|
{ |
85
|
|
|
self::getInstance(); |
86
|
|
|
try { |
87
|
|
|
$stmt = self::select($tbl_name, $params); |
88
|
|
|
$final = $stmt->fetch(); |
89
|
|
|
return $final; |
90
|
|
|
} catch (PDOException $e) { |
91
|
|
|
//Your catch exception goes here. |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $tbl_name |
97
|
|
|
* @param $params |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public static function fetchAll($tbl_name, $params) |
101
|
|
|
{ |
102
|
|
|
self::getInstance(); |
103
|
|
|
try { |
104
|
|
|
$stmt = self::select($tbl_name, $params); |
105
|
|
|
$final = $stmt->fetchAll(); |
106
|
|
|
return $final; |
107
|
|
|
} catch (PDOException $e) { |
108
|
|
|
echo 'Failed to fetch data.'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $tbl_name |
115
|
|
|
* @param $params |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public static function getAll($tbl_name, $params) |
119
|
|
|
{ |
120
|
|
|
self::getInstance(); |
121
|
|
|
$all = self::fetchAll($tbl_name, $params); |
122
|
|
|
return $all; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param $tbl_name |
127
|
|
|
* @param $id |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
|
|
public static function getByID($tbl_name, $id) |
131
|
|
|
{ |
132
|
|
|
self::getInstance(); |
133
|
|
|
$params = [ |
134
|
|
|
'where' => [ |
135
|
|
|
'column' => 'id', |
136
|
|
|
'operator' => '=', |
137
|
|
|
'value' => $id |
138
|
|
|
] |
139
|
|
|
]; |
140
|
|
|
$all = self::fetch($tbl_name, $params); |
141
|
|
|
return $all; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param $tbl_name |
146
|
|
|
* @param $params |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
|
|
public static function first($tbl_name, $params) |
150
|
|
|
{ |
151
|
|
|
self::getInstance(); |
152
|
|
|
$stats = self::fetchAll($tbl_name, $params); |
153
|
|
|
return $stats[0]; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param array $array |
158
|
|
|
* @return string |
159
|
|
|
*/ |
160
|
|
|
private static function get_bind($array = array()) |
161
|
|
|
{ |
162
|
|
|
$keys = array_keys($array); |
163
|
|
|
$values = null; |
164
|
|
|
foreach (array_slice($keys, 0, count($array) - 1) as $key => $value) { |
165
|
|
|
$values .= ':' . $value . ', '; |
166
|
|
|
} |
167
|
|
|
$all = $values . ':' . end($keys); |
168
|
|
|
return $all; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $array |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
View Code Duplication |
private static function get_bind_exe($array = array()) |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
$keys = array_keys($array); |
178
|
|
|
$values = null; |
179
|
|
|
foreach (array_slice($keys, 0, count($array) - 1) as $key => $value) { |
180
|
|
|
$values [] = ':' . $value; |
181
|
|
|
} |
182
|
|
|
$values[] = ':' . end($keys); |
183
|
|
|
return $values; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $array |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
|
|
private static function get_values($array = array()) |
191
|
|
|
{ |
192
|
|
|
$keys = array_values($array); |
193
|
|
|
return $keys; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param array $array |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
View Code Duplication |
private static function get_keys($array = array()) |
|
|
|
|
201
|
|
|
{ |
202
|
|
|
$keys = array_keys($array); |
203
|
|
|
$values = null; |
204
|
|
|
foreach (array_slice($keys, 0, count($array) - 1) as $key => $value) { |
205
|
|
|
$values .= $value . ', '; |
206
|
|
|
} |
207
|
|
|
$all = $values . end($keys); |
208
|
|
|
return $all; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param $tbl_name |
214
|
|
|
* @param array $params |
215
|
|
|
* @return boolean|null |
216
|
|
|
*/ |
217
|
|
|
public static function Create($tbl_name, $params = array()) |
218
|
|
|
{ |
219
|
|
|
self::getInstance(); |
220
|
|
|
$keys = self::get_keys($params); |
221
|
|
|
$bind = self::get_bind($params); |
222
|
|
|
$string = "INSERT INTO $tbl_name (" . $keys . ") VALUES (" . $bind . ")"; |
223
|
|
|
$values = self::get_values($params); |
224
|
|
|
$bind_2 = self::get_bind_exe($params); |
225
|
|
|
$bindp = array_combine($bind_2, $values); |
226
|
|
|
try { |
227
|
|
|
$stmt = self::$instance->prepare($string); |
228
|
|
|
self::bindParam($stmt, $bindp); |
229
|
|
|
$stmt->execute(); |
230
|
|
|
} catch |
231
|
|
|
(PDOException $e) { |
232
|
|
|
echo 'Failed to insert Data.'; |
233
|
|
|
echo $e->getMessage(); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $tbl_name |
239
|
|
|
* @param $condition |
240
|
|
|
*/ |
241
|
|
|
public static function Delete($tbl_name, $condition) |
242
|
|
|
{ |
243
|
|
|
self::getInstance(); |
244
|
|
|
$sql = "DELETE FROM $tbl_name WHERE {$condition['column']} {$condition['operator']} {$condition['value']}"; |
245
|
|
|
$key = ":" . $condition['column']; |
246
|
|
|
$value = $condition['value']; |
247
|
|
|
try { |
248
|
|
|
$sql = self::$instance->prepare($sql); |
249
|
|
|
$sql->bindParam($key, $value); |
250
|
|
|
$sql->execute(); |
251
|
|
|
} catch |
252
|
|
|
(PDOException $e) { |
253
|
|
|
echo 'Failed to Delete Data.'; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @param $array |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
|
|
public static function update_array($array) |
262
|
|
|
{ |
263
|
|
|
$statement = NULL; |
264
|
|
|
foreach (array_slice($array, 0, count($array) - 1, true) as $key => $value) { |
265
|
|
|
$statement .= $key . '=' . ":" . $key . ","; |
266
|
|
|
} |
267
|
|
|
end($array); |
268
|
|
|
$all = $statement . key($array) . ' = :' . key($array); |
269
|
|
|
return $all; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param $tbl_name |
274
|
|
|
* @param $array |
275
|
|
|
* @param $where |
276
|
|
|
* @return boolean|null |
277
|
|
|
*/ |
278
|
|
|
public static function Update($tbl_name, $array, $where) |
279
|
|
|
{ |
280
|
|
|
self::getInstance(); |
281
|
|
|
$sql = "UPDATE $tbl_name SET " . self::update_array($array) . ' ' . "WHERE {$where['column']} {$where['operator']} " . ':' . "{$where['column']}"; |
282
|
|
|
$sql = self::$instance->prepare($sql); |
283
|
|
|
$key = ":" . $where['column']; |
284
|
|
|
$value = $where['value']; |
285
|
|
|
$sql->bindParam($key, $value); |
286
|
|
|
$values = self::get_values($array); |
287
|
|
|
$bind_2 = self::get_bind_exe($array); |
288
|
|
|
$bindp = array_combine($bind_2, $values); |
289
|
|
|
self::bindParam($sql, $bindp); |
290
|
|
|
|
291
|
|
|
try { |
292
|
|
|
if ($sql->execute()) { |
293
|
|
|
return true; |
294
|
|
|
} else { |
295
|
|
|
return false; |
296
|
|
|
} |
297
|
|
|
} catch |
298
|
|
|
(PDOException $e) { |
299
|
|
|
echo 'Failed to Update Data.'; |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param \PDOStatement $stmt |
305
|
|
|
* @param $bind |
306
|
|
|
*/ |
307
|
|
|
private static function bindParam($stmt, $bind) |
308
|
|
|
{ |
309
|
|
|
foreach ($bind as $key => $value) { |
310
|
|
|
$stmt->bindValue($key, $value); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param $statment |
316
|
|
|
* @return bool|mixed |
317
|
|
|
*/ |
318
|
|
|
public static function Query($statment) |
319
|
|
|
{ |
320
|
|
|
self::getInstance(); |
321
|
|
|
$string = $statment; |
322
|
|
|
$stmt = self::$instance->prepare($string); |
323
|
|
|
$stmt->execute(); |
324
|
|
|
if (strtolower(substr($statment, 0, 6)) == 'select') { |
325
|
|
|
$stmt->setFetchMode(PDO::FETCH_OBJ); |
326
|
|
|
$result = $stmt->fetch(); |
327
|
|
|
return $result; |
328
|
|
|
} else { |
329
|
|
|
if ($stmt) { |
330
|
|
|
return true; |
331
|
|
|
} else { |
332
|
|
|
return false; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
protected function __clone() |
338
|
|
|
{ |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
protected function __wakeup() |
342
|
|
|
{ |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.