-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbpdo.class.php
More file actions
408 lines (347 loc) · 10.9 KB
/
Dbpdo.class.php
File metadata and controls
408 lines (347 loc) · 10.9 KB
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
<?php
/**
* DB - A simple database class which also logs errors.
* You also need config.php file to define the database configuration settigs.
*
* This class will do SELECT, UPDATE, INSERT and DELETE statements.
*
*/
require("Log.class.php");
class DB
{
private $pdo; # @object, The PDO object.
private $sQuery; # @object, PDO statement object.
private $bConnected = false; # @bool , Connected to the database.
private $log; # @object, Object for logging exceptions.
private $parameters; # @array, The parameters of the SQL query.
private $debug = false; # Set to true to show debug info.
private $logging = false; # Set to true to send log details to "logs" folder.
/***********************************************************
* Default Constructor
*
* 1. Instantiate Log class.
* 2. Connect to database.
* 3. Creates the parameter array.
*/
public function __construct()
{
$this->log = new Log();
$this->Connect();
$this->parameters = array();
}
/***********************************************************
* This method makes connection to the database.
*
* 1. Reads the database settings from a config file.
* 2. Puts the config contents into the settings array.
* 3. Tries to connect to the database.
* 4. If connection failed, exception is displayed and a log file gets created.
*/
private function Connect()
{
$config_file_path = "config.php";
include( $config_file_path );
//echo($dbname);
//echo($dbhost);
//echo($dbusername);
//echo($dbpass);
//exit();
$dsn = 'mysql:dbname='.$dbname.';host='.$dbhost.'';
try
{
# Read settings from config file, set UTF8
$this->pdo = new PDO($dsn, $dbusername, $dbpass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
# We can now log any exceptions on Fatal error.
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//# Disable emulation of prepared statements, use REAL prepared statements instead.
//$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
# Connection succeeded, set the boolean to true.
$this->bConnected = true;
}
catch (PDOException $e)
{
# Write into log
if ($this->logging) {
echo $this->ExceptionLog($e->getMessage());
}
die();
}
}
/***********************************************************
*
* You can use this method if you want to close the PDO connection
* If you don't do this explicitly, PHP will automatically close the
* connection when your script ends.
*
*/
public function CloseConnection()
{
# Set the PDO object to null to close the connection
# http://www.php.net/manual/en/pdo.connections.php
$this->pdo = null;
}
/***********************************************************
* Every method which needs to execute a SQL query uses this method.
*
* 1. If not connected, connect to the database.
* 2. Prepare Query.
* 3. Parameterize Query.
* 4. Execute Query.
* 5. On exception : Write Exception into the log + SQL query.
* 6. Reset the Parameters.
*/
private function Init($query, $parameters = "")
{
# Connect to database
if(!$this->bConnected) { $this->Connect(); }
try {
if ($this->debug) {
echo($query . "<br/>");
}
# Prepare query
$this->sQuery = $this->pdo->prepare($query);
# Add parameters to the parameter array
$this->bindMore($parameters);
//This is where we need to get the array and get the individual components.
# Bind parameters
if(!empty($this->parameters)) {
foreach($this->parameters as $param)
{
$parameters = explode("\x7F", $param);
$countparas = count($parameters);
switch ($countparas) {
case $countparas:
case 2:
$this->sQuery->bindParam( $parameters[0], $parameters[1] ); //No optional parameters
break;
case 3:
$this->sQuery->bindParam( $parameters[0], $parameters[1], $parameters[2] ); //Only the type for the optional paramenter
break;
case 4:
$this->sQuery->bindParam( $parameters[0], $parameters[1], $parameters[2], $parameters[3] ); //Both type and length optional parameter
break;
default:
# Write into log
if ($this->logging) {
echo $this->ExceptionLog("Error in parameters");
}
die();
break;
}
if ($this->debug) {
echo("<strong>para0:</strong> " . $parameters[0] . " <strong>para1:</strong> " . $parameters[1] . " <strong>para2:</strong> " .$parameters[2] . " <strong>para3:</strong> " .$parameters[3] ."<br/>");
}
}
}
# Execute SQL
$this->success = $this->sQuery->execute();
}
catch(PDOException $e)
{
# Write into log and display Exception
if ($this->logging) {
echo $this->ExceptionLog($e->getMessage(), $query );
}
die();
}
# Reset the parameters
$this->parameters = array();
}
/***********************************************************
* @void
*
* Add the parameter to the parameter array
* @param string $para - the name of the parameter
* @param string $value - the value of the parameter
* @param string $type - the type of data
*/
public function bind($para, $value, $type, $length)
{
if ($type == "" && $length == "") { //No optional parameters
if ($this->debug) {
echo("No optional parameters<br/>");
}
$this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value;
}
elseif ($type != "" && $length == "") { //Only the type for the optional paramenter
if ($this->debug) {
echo("Only the type for the optional paramenter<br/>");
}
$this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value . "\x7F" . $type;
}
elseif ($type != "" && $length != "") { //Both type and length optional parameter
if ($this->debug) {
echo("Both type and length optional parameter<br/>");
}
$this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value . "\x7F" . $type . "\x7F" . $length;
}
else {
# Write into log
if ($this->logging) {
echo $this->ExceptionLog("Error with type length in the array");
}
die();
}
if ($this->debug) {
echo("<strong>para:</strong> " . $para . " <strong>value:</strong> " . $value . " <strong>type:</strong> " . $type . " <strong>length</strong> " . $length . "<br/>");
}
}
/***********************************************************
* @void
*
* Add more parameters to the parameter array
* @param array $parray
*/
public function bindMore($parray)
{
if(empty($this->parameters) && is_array($parray)) {
foreach($parray as $key => $value) {
//echo("elements in the array: " . count($value) . "<br/>");
$countvalue = count($value);
$i = 0;
$keyvalue = "";
$paramvalue = "";
$typevalue = "";
$lengthvalue = "";
foreach ( $value as $key => $param) {
switch ($i) {
case 0:
$keyvalue = $key;
$paramvalue = $param;
break;
case 1:
$typevalue = $param;
break;
case 2:
$lengthvalue = $param;
break;
default:
# Write into log
if ($this->logging) {
echo $this->ExceptionLog("Error in parameters");
}
die();
break;
}
$i++;
if ($i == 3) {$i = 0;}
}
$this->bind( $keyvalue, $paramvalue, $typevalue, $lengthvalue );
}
}
}
/***********************************************************
* If the SQL query contains a SELECT or SHOW statement it returns an array containing all of the result set row
* If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows
*
* @param string $query
* @param array $params
* @param int $fetchmode
* @return mixed
*/
public function query($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
$query = trim($query);
$this->Init($query, $params);
$rawStatement = explode(" ", $query);
# Which SQL statement is used
$statement = strtolower($rawStatement[0]);
if ($statement === 'select' || $statement === 'show') {
return $this->sQuery->fetchAll($fetchmode);
}
elseif ( $statement === 'insert' || $statement === 'update' || $statement === 'delete' ) {
return $this->sQuery->rowCount();
}
else {
return NULL;
}
}
/***********************************************************
* Returns the last inserted id.
* @return string
*/
public function lastInsertId() {
return $this->pdo->lastInsertId();
}
/***********************************************************
* Returns an array which represents a column from the result set
*
* @param string $query
* @param array $params
* @return array
*/
public function column($query, $params = null)
{
$this->Init($query,$params);
$Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM);
$column = null;
foreach($Columns as $cells) {
$column[] = $cells[0];
}
return $column;
}
/***********************************************************
* Returns an array which represents a row from the result set
*
* @param string $query
* @param array $params
* @param int $fetchmode
* @return array
*/
public function row($query, $params = null, $fetchmode = PDO::FETCH_ASSOC)
{
$this->Init($query, $params);
return $this->sQuery->fetch($fetchmode);
}
/***********************************************************
* Returns the value of one single field/column
*
* @param string $query
* @param array $params
* @return string
*/
public function single($query, $params = null)
{
$this->Init($query,$params);
return $this->sQuery->fetchColumn();
}
/***********************************************************
* Sets the debug value to true or false - default is false
*
* @param boolean $debug
* @return void
*/
public function setdebug($debug) {
$this->debug = $debug;
}
/***********************************************************
* Sets the logging value to true or false - default is false
*
* @param boolean $logging
* @return void
*/
public function setlogging($debug) {
$this->logging = $logging;
}
/***********************************************************
* Writes the log and returns the exception
*
* @param string $message
* @param string $sql
* @return string
*/
private function ExceptionLog($message, $sql = "")
{
$exception = 'Unhandled Exception. <br />';
$exception .= $message;
$exception .= "<br /> You can find the error back in the log.";
if(!empty($sql)) {
# Add the Raw SQL to the Log
$message .= "\r\nRaw SQL : " . $sql;
}
# Write into log
$this->log->write($message);
return $exception;
}
}
?>