-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBPDO.php
94 lines (75 loc) · 1.94 KB
/
DBPDO.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
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
<?php
class DBPDO {
public $pdo;
private $error;
function __construct() {
$this->connect();
}
function prep_query($query){
return $this->pdo->prepare($query);
}
function connect(){
if(!$this->pdo){
$dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST;
$user = DATABASE_USER;
$password = DATABASE_PASS;
try {
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
return true;
} catch (PDOException $e) {
$this->error = $e->getMessage();
die($this->error);
return false;
}
}else{
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
return true;
}
}
function table_exists($table_name){
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
$stmt->execute(array($this->add_table_prefix($table_name)));
return $stmt->rowCount() > 0;
}
function execute($query, $values = null){
if($values == null){
$values = array();
}else if(!is_array($values)){
$values = array($values);
}
$stmt = $this->prep_query($query);
$stmt->execute($values);
return $stmt;
}
function fetch($query, $values = null){
if($values == null){
$values = array();
}else if(!is_array($values)){
$values = array($values);
}
$stmt = $this->execute($query, $values);
return $stmt->fetch(PDO::FETCH_ASSOC);
}
function fetchAll($query, $values = null, $key = null){
if($values == null){
$values = array();
}else if(!is_array($values)){
$values = array($values);
}
$stmt = $this->execute($query, $values);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Allows the user to retrieve results using a
// column from the results as a key for the array
if($key != null && $results[0][$key]){
$keyed_results = array();
foreach($results as $result){
$keyed_results[$result[$key]] = $result;
}
$results = $keyed_results;
}
return $results;
}
function lastInsertId(){
return $this->pdo->lastInsertId();
}
}