-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdb.php
50 lines (47 loc) · 1.62 KB
/
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
<?php
require_once("config.php");
try {
$db = new PDO($CFG->pdo, $CFG->dbuser, $CFG->dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
error_log("DB connection: "+$ex->getMessage());
die($ex->getMessage());
}
// Run a PDO Query with lots of error checking
// TODO: Work in progress
function pdoQuery($db, $sql, $arr=FALSE, $log_error=TRUE) {
$errormode = $db->getAttribute(PDO::ATTR_ERRMODE);
if ( $errormode != PDO::ERRMODE_EXCEPTION) {
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
$q = FALSE;
$success = FALSE;
$message = '';
if ( $arr !== FALSE && ! is_array($arr) ) $arr = Array($arr);
$start = microtime(true);
debugLog($sql, $arr);
try {
$q = $db->prepare($sql);
if ( $arr === FALSE ) {
$success = $q->execute();
} else {
$success = $q->execute($arr);
}
} catch(Exception $e) {
$success = FALSE;
$message = $e->getMessage();
}
if ( ! is_object($q) ) $q = stdClass();
if ( isset( $q->success ) ) die("PDO::Statement should not have success member");
$q->success = $success;
if ( isset( $q->ellapsed_time ) ) die("PDO::Statement should not have success member");
$q->ellapsed_time = $microtime(true)-$start;
// In case we build this...
if ( !isset($q->errorCode) ) $q->errorCode = '42000';
if ( !isset($q->errorInfo) ) $q->errorInfo = Array('42000', '42000', $message);
// Restore ERRMODE if we changed it
if ( $errormode != PDO::ERRMODE_EXCEPTION) {
$db->setAttribute(PDO::ATTR_ERRMODE, $errormode);
}
return $q;
}