1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Database Class |
5
|
|
|
* |
6
|
|
|
* This class provides variety of methods that hides complexity |
7
|
|
|
* |
8
|
|
|
* So, you can prepare, bind, and execute queries in a very handy way, |
9
|
|
|
* since you don't need to care about dealing $this->statement, or $this-connection property |
10
|
|
|
* |
11
|
|
|
* Methods like getById, getByEmail, getByUserId, getByUserEmail, deleteById, countAll, ...etc |
12
|
|
|
* are also very useful and will save you from writing the same query every time |
13
|
|
|
* |
14
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
15
|
|
|
* @author Omar El Gabry <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class Database { |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @access public |
22
|
|
|
* @var PDO PDO Object |
23
|
|
|
*/ |
24
|
|
|
private $connection = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @access public |
28
|
|
|
* @var PDOStatement PDOStatement Object |
29
|
|
|
*/ |
30
|
|
|
private $statement = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @access public |
34
|
|
|
* @static static |
35
|
|
|
* @var Database Database Object used to implement the Singleton pattern |
36
|
|
|
*/ |
37
|
|
|
private static $database = null; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* This is the constructor for Database Object. |
41
|
|
|
* |
42
|
|
|
* @access private |
43
|
|
|
*/ |
44
|
|
|
private function __construct() { |
45
|
|
|
|
46
|
|
|
if ($this->connection === null) { |
47
|
|
|
|
48
|
|
|
$this->connection = new PDO('mysql:dbname='.Config::get('DB_NAME').';host='.Config::get('DB_HOST').';charset='.Config::get('DB_CHARSET'), Config::get('DB_USER'), Config::get('DB_PASS')); |
49
|
|
|
|
50
|
|
|
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
51
|
|
|
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This method for instantiating database object using the Singleton Design pattern. |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @static static method |
61
|
|
|
* @return Database Instantiate(if not already instantiated) |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
public static function openConnection(){ |
65
|
|
|
if(self::$database === null){ |
66
|
|
|
self::$database = new Database(); |
67
|
|
|
} |
68
|
|
|
return self::$database; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prepares a SQL query for execution and assign a statement object to $this->statement |
73
|
|
|
* |
74
|
|
|
* @access public |
75
|
|
|
* @param string $query |
76
|
|
|
* |
77
|
|
|
*/ |
78
|
|
|
public function prepare($query) { |
79
|
|
|
$this->statement = $this->connection->prepare($query); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Binds a value to a parameter. |
84
|
|
|
* |
85
|
|
|
* A better practice to explicitly define data types in parameter declarations, |
86
|
|
|
* So, instead of defining the data type parameter every time, |
87
|
|
|
* Just pass the value, and getPDOType() will take care of it's data type |
88
|
|
|
* |
89
|
|
|
* This is the same for bindParam() |
90
|
|
|
* |
91
|
|
|
* @access public |
92
|
|
|
* @param string $param |
93
|
|
|
* @param mixed $value |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
|
|
public function bindValue($param, $value) { |
97
|
|
|
$type = self::getPDOType($value); |
98
|
|
|
$this->statement->bindValue($param, $value, $type); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Binds variable by reference to a parameter. |
103
|
|
|
* |
104
|
|
|
* @access public |
105
|
|
|
* @param string $param |
106
|
|
|
* @param mixed $var |
107
|
|
|
* |
108
|
|
|
*/ |
109
|
|
|
public function bindParam($param, &$var) { |
110
|
|
|
$type = self::getPDOType($var); |
111
|
|
|
$this->statement->bindParam($param, $var, $type); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Executes a prepared statement |
116
|
|
|
* |
117
|
|
|
* @access public |
118
|
|
|
* @param array Array of values to be bound in SQL query, All values are treated as PDO::PARAM_STR. |
119
|
|
|
* @return boolean Returns TRUE on success or FALSE on failure. |
120
|
|
|
* |
121
|
|
|
*/ |
122
|
|
|
public function execute($arr = null){ |
123
|
|
|
if($arr === null) return $this->statement->execute(); |
124
|
|
|
else return $this->statement->execute($arr); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* To fetch only a single column in form of 0-indexed array. |
129
|
|
|
* |
130
|
|
|
* @access public |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function fetchColumn() { |
134
|
|
|
return $this->statement->fetchAll(PDO::FETCH_COLUMN, 0); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* To fetch the result data in form of [0-indexed][key][value] array. |
139
|
|
|
* |
140
|
|
|
* @access public |
141
|
|
|
* @return array empty array if no data returned |
142
|
|
|
*/ |
143
|
|
|
public function fetchAllAssociative() { |
144
|
|
|
return $this->statement->fetchAll(PDO::FETCH_ASSOC); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* To fetch Only the next row from the result data in form of [key][value] array. |
149
|
|
|
* |
150
|
|
|
* @access public |
151
|
|
|
* @return array|bool false on if no data returned |
152
|
|
|
*/ |
153
|
|
|
public function fetchAssociative() { |
154
|
|
|
return $this->statement->fetch(PDO::FETCH_ASSOC); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* To fetch All the data in form of [0-indexed][an anonymous object with property names that correspond to the column names] array. |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* @return array |
162
|
|
|
*/ |
163
|
|
|
public function fetchAllObject() { |
164
|
|
|
return $this->statement->fetchAll(PDO::FETCH_OBJ); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* To fetch Only the next row from the result data in form of an anonymous object with property names that correspond to the column names. |
169
|
|
|
* |
170
|
|
|
* @access public |
171
|
|
|
* @return object |
172
|
|
|
*/ |
173
|
|
|
public function fetchObject() { |
174
|
|
|
return $this->statement->fetch(PDO::FETCH_OBJ); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* To fetch All data in form of an array indexed by both column name and 0-indexed column |
179
|
|
|
* |
180
|
|
|
* @access public |
181
|
|
|
* @return array |
182
|
|
|
*/ |
183
|
|
|
public function fetchAllBoth() { |
184
|
|
|
return $this->statement->fetchAll(PDO::FETCH_BOTH); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* To fetch Only the next row from the result data in form of an array indexed by both column name and 0-indexed column |
189
|
|
|
* |
190
|
|
|
* @access public |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public function fetchBoth() { |
194
|
|
|
return $this->statement->fetch(PDO::FETCH_BOTH); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Returns the ID of the last inserted row or sequence value |
199
|
|
|
* "This method may not return a meaningful or consistent result across different PDO drivers" |
200
|
|
|
* |
201
|
|
|
* @access public |
202
|
|
|
* @return integer The ID of the last inserted row of Auto-incremented primary key. |
203
|
|
|
* @see http://php.net/manual/en/pdo.lastinsertid.php |
204
|
|
|
* |
205
|
|
|
*/ |
206
|
|
|
public function lastInsertedId() { |
207
|
|
|
return $this->connection->lastInsertId(); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Start a transaction |
212
|
|
|
* |
213
|
|
|
* @access public |
214
|
|
|
* @see http://php.net/manual/en/pdo.begintransaction.php |
215
|
|
|
* @see http://stackoverflow.com/questions/10155946/php-pdo-transactions |
216
|
|
|
*/ |
217
|
|
|
public function beginTransaction() { |
218
|
|
|
$this->connection->beginTransaction(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Commit a transaction. This method will be called after beginTransaction() |
223
|
|
|
* |
224
|
|
|
* It will return the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction. |
225
|
|
|
* |
226
|
|
|
* @access public |
227
|
|
|
* @see http://php.net/manual/en/pdo.commit.php |
228
|
|
|
*/ |
229
|
|
|
public function commit() { |
230
|
|
|
$this->connection->commit(); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Rollback a transaction. This method will be called after beginTransaction() |
235
|
|
|
* A PDOException will be thrown if no transaction is active. |
236
|
|
|
* |
237
|
|
|
* It will return the database connection to autocommit mode until the next call to PDO::beginTransaction() starts a new transaction. |
238
|
|
|
* |
239
|
|
|
* @access public |
240
|
|
|
* @see http://php.net/manual/en/pdo.rollback.php |
241
|
|
|
*/ |
242
|
|
|
public function rollBack() { |
243
|
|
|
$this->connection->rollBack(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns the number of rows affected by the last SQL statement |
248
|
|
|
* "If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement" |
249
|
|
|
* |
250
|
|
|
* @access public |
251
|
|
|
* @see http://php.net/manual/en/pdostatement.rowcount.php |
252
|
|
|
*/ |
253
|
|
|
public function countRows() { |
254
|
|
|
return $this->statement->rowCount(); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Counts the number of rows in a specific table |
259
|
|
|
* |
260
|
|
|
* @access public |
261
|
|
|
* @param string $table |
262
|
|
|
* @return integer |
263
|
|
|
* |
264
|
|
|
*/ |
265
|
|
|
public function countAll($table){ |
266
|
|
|
$this->statement = $this->connection->prepare('SELECT COUNT(*) AS count FROM '.$table); |
267
|
|
|
$this->execute(); |
268
|
|
|
return (int)$this->fetchAssociative()["count"]; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Select all rows from a table |
273
|
|
|
* |
274
|
|
|
* @access public |
275
|
|
|
* @param string $table |
276
|
|
|
* |
277
|
|
|
*/ |
278
|
|
|
public function getAll($table){ |
279
|
|
|
$this->statement = $this->connection->prepare('SELECT * FROM '.$table); |
280
|
|
|
$this->execute(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Select a row from a table provided by id(primary key) |
285
|
|
|
* |
286
|
|
|
* @access public |
287
|
|
|
* @param string $table |
288
|
|
|
* @param mixed $id |
289
|
|
|
* |
290
|
|
|
*/ |
291
|
|
|
public function getById($table, $id){ |
292
|
|
|
$this->statement = $this->connection->prepare('SELECT * FROM '.$table. ' WHERE id = :id LIMIT 1'); |
293
|
|
|
$this->bindValue(':id', $id); |
294
|
|
|
$this->execute(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Select a row from a table provided by email |
299
|
|
|
* |
300
|
|
|
* @access public |
301
|
|
|
* @param string $table |
302
|
|
|
* @param string $email |
303
|
|
|
* |
304
|
|
|
*/ |
305
|
|
|
public function getByEmail($table, $email){ |
306
|
|
|
$this->statement = $this->connection->prepare('SELECT * FROM '.$table. ' WHERE email = :email LIMIT 1'); |
307
|
|
|
$this->bindValue(':email', $email); |
308
|
|
|
$this->execute(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Select all rows from a table provided by user id |
313
|
|
|
* |
314
|
|
|
* @access public |
315
|
|
|
* @param string $table |
316
|
|
|
* @param integer $userId |
317
|
|
|
* |
318
|
|
|
*/ |
319
|
|
|
public function getByUserId($table, $userId){ |
320
|
|
|
$this->statement = $this->connection->prepare('SELECT * FROM '.$table. ' WHERE user_id = :user_id'); |
321
|
|
|
$this->bindValue(':user_id', $userId); |
322
|
|
|
$this->execute(); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Select all rows from a table provided by user email |
327
|
|
|
* |
328
|
|
|
* @access public |
329
|
|
|
* @param string $table |
330
|
|
|
* @param string $user_email |
331
|
|
|
* |
332
|
|
|
*/ |
333
|
|
|
public function getByUserEmail($table, $user_email){ |
334
|
|
|
$this->statement = $this->connection->prepare('SELECT * FROM '.$table. ' WHERE user_email = :user_email'); |
335
|
|
|
$this->bindValue(':user_email', $user_email); |
336
|
|
|
$this->execute(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Delete all rows from a table |
341
|
|
|
* |
342
|
|
|
* @access public |
343
|
|
|
* @param string $table |
344
|
|
|
* |
345
|
|
|
*/ |
346
|
|
|
public function deleteAll($table){ |
347
|
|
|
$this->statement = $this->connection->prepare('DELETE FROM '.$table); |
348
|
|
|
$this->execute(); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Delete all data from a table provided by id(primary key) |
353
|
|
|
* |
354
|
|
|
* @access public |
355
|
|
|
* @param string $table |
356
|
|
|
* @param mixed $id |
357
|
|
|
* |
358
|
|
|
*/ |
359
|
|
|
public function deleteById($table, $id){ |
360
|
|
|
$this->statement = $this->connection->prepare('DELETE FROM '.$table. ' WHERE id = :id LIMIT 1'); |
361
|
|
|
$this->bindValue(':id', $id); |
362
|
|
|
$this->execute(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Determine the PDOType of a passed value. |
367
|
|
|
* This is done by determining the data type of the passed value. |
368
|
|
|
* |
369
|
|
|
* @access public |
370
|
|
|
* @param mixed $value |
371
|
|
|
* @return integer PDO Constants |
372
|
|
|
* |
373
|
|
|
*/ |
374
|
|
|
private static function getPDOType($value){ |
375
|
|
|
switch ($value) { |
376
|
|
|
case is_int($value): |
377
|
|
|
return PDO::PARAM_INT; |
378
|
|
|
case is_bool($value): |
379
|
|
|
return PDO::PARAM_BOOL; |
380
|
|
|
case is_null($value): |
381
|
|
|
return PDO::PARAM_NULL; |
382
|
|
|
default: |
383
|
|
|
return PDO::PARAM_STR; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Closing the connection. |
389
|
|
|
* |
390
|
|
|
* It's not necessary to close the connection, however it's a good practice. |
391
|
|
|
* "If you don't do this explicitly, PHP will automatically close the connection when your script ends." |
392
|
|
|
* |
393
|
|
|
* This will be used at the end "footer.php" |
394
|
|
|
* |
395
|
|
|
* @static static method |
396
|
|
|
* @access public |
397
|
|
|
* @see http://php.net/manual/en/pdo.connections.php |
398
|
|
|
*/ |
399
|
|
|
public static function closeConnection() { |
400
|
|
|
if(isset(self::$database)) { |
401
|
|
|
self::$database->connection = null; |
402
|
|
|
self::$database->statement = null; |
403
|
|
|
self::$database = null; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
} |
408
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.