-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDB.class.php
More file actions
128 lines (119 loc) · 4.01 KB
/
DB.class.php
File metadata and controls
128 lines (119 loc) · 4.01 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
<?php
/**
* This class provides some handy wrapper functions for accessing databases.
* All functions are injection-proof and use prepared statements where possible.
*/
define(CONFIG, './config/');
class DB {
static function last_error() {
return 'ERROR CODE: ' . self::$last_error[0];
}
static function insert($string, $params, $id_name = '') {
try {
self::connect();
$prep = self::$dbh->prepare($string);
if ($prep->execute($params)) {
$id = self::$dbh->lastInsertId($id_name);
self::disconnect();
return $id;
} else {
self::$last_error = self::$dbh->errorInfo();
self::disconnect();
return false;
}
} catch (PDOException $exc) {
self::$last_error = $exc;
return false;
}
}
static function insert_all($db_obj_arr) {
self::connect();
$success = true;
$total = 0;
foreach ($db_obj_arr as $db_obj) {
$success = true;
try {
$map = $db_obj->fields();
$table = $db_obj->table();
$fields = implode(', ', array_keys($map));
$names_arr = array();
foreach (array_keys($map) as $field) {
$names_arr []= '?';
}
$names = implode(', ', $values_arr);
$sql = 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES (' .
$names . ')';
$values = array();
foreach (array_keys($map) as $field) {
$values []= $map[$field];
}
$stm = self::$dbh->prepare($sql);
self::$dbh->execute($stm, $values);
} catch (PDOException $exc) {
$success = false;
}
$total += $success ? 1 : 0;
}
self::disconnect();
return $total;
}
static function query($string, $params) {
self::connect();
$prep = self::$dbh->prepare($string);
$result = array();
if ($prep->execute($params)) {
while ($row = $prep->fetch(PDO::FETCH_ASSOC)) {
$result []= $row;
}
} else {
self::$last_error = self::$dbh->errorInfo();
return false;
}
self::disconnect();
return $result;
}
static function delete($string, $params) {
self::connect();
$prep = self::$dbh->prepare($string);
if ($prep->execute($params)) {
self::disconnect();
return $prep->rowCount();
} else {
self::$last_error = $self::$dbh->errorInfo();
self::disconnect();
return false;
}
}
static function update($string, $params) {
self::connect();
$prep = self::$dbh->prepare($string);
if ($prep->execute($params)) {
self::disconnect();
return $prep->rowCount();
} else {
self::$last_error = self::$dbh->errorInfo();
self::disconnect();
return false;
}
}
private static function connect() {
$xml = simplexml_load_file(CONFIG . 'conf.xml');
$db_node = $xml->databases->database;
$host = (string)$db_node->host;
$db_name = (string)$db_node->database;
$user = (string)$db_node->user;
$pass = (string)$db_node->password;
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
self::$dbh = new PDO('mysql:host=' . $host . ';dbname=' .
$db_name, $user, $pass, $options);
} catch (PDOException $e) {
echo $e;
}
}
private static function disconnect() {
self::$dbh = null;
}
private static $dbh;
private static $last_error;
}