-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.class.php
169 lines (155 loc) · 3.99 KB
/
db.class.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
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
<?php
/**
* Simple PHP PDO Class
* @author Miks Zvirbulis (twitter.com/MiksZvirbulis)
* @version 1.1
* 1.0 - First version launched. Allows access to one database and a few regular functions have been created.
* 1.1 - Added a constructor which allows multiple databases to be called on different variables.
*/
class db{
# Database host address, defined in construction.
protected $host;
# Username for authentication, defined in construction.
protected $username;
# Password for authentication, defined in construction.
protected $password;
# Database name, defined in construction.
protected $database;
# Connection variable. DO NOT CHANGE!
protected $connection;
# @bool default for this is to be left to FALSE, please. This determines the connection state.
public $connected = false;
# @bool this controls if the errors are displayed. By default, this is set to true.
private $errors = true;
function __construct($db_host, $db_username, $db_password, $db_database){
global $c;
try{
$this->host = $db_host;
$this->username = $db_username;
$this->password = $db_password;
$this->database = $db_database;
$this->connected = true;
$this->connection = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->database, $this->username, $this->password);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
catch(PDOException $e){
$this->connected = false;
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}
function __destruct(){
$this->connected = false;
$this->connection = null;
}
public function error($error){
echo $error;
}
public function fetch($query, $parameters = array()){
if($this->connected === true){
try{
$query = $this->connection->prepare($query);
$query->execute($parameters);
return $query->fetch();
}
catch(PDOException $e){
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}else{
return false;
}
}
public function fetchAll($query, $parameters = array()){
if($this->connected === true){
try{
$query = $this->connection->prepare($query);
$query->execute($parameters);
return $query->fetchAll();
}
catch(PDOException $e){
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}else{
return false;
}
}
public function count($query, $parameters = array()){
if($this->connected === true){
try{
$query = $this->connection->prepare($query);
$query->execute($parameters);
return $query->rowCount();
}
catch(PDOException $e){
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}else{
return false;
}
}
public function insert($query, $parameters = array()){
if($this->connected === true){
try{
$query = $this->connection->prepare($query);
$query->execute($parameters);
return true;
}
catch(PDOException $e){
if($this->errors === true){
echo $this->error($e->getMessage());
}
return false;
}
}else{
return false;
}
}
public function update($query, $parameters = array()){
if($this->connected === true){
return $this->insert($query, $parameters);
}else{
return false;
}
}
public function delete($query, $parameters = array()){
if($this->connected === true){
return $this->insert($query, $parameters);
}else{
return false;
}
}
public function tableExists($table){
if($this->connected === true){
try{
$query = $this->count("SHOW TABLES LIKE '$table'");
return ($query > 0) ? true : false;
}
catch(PDOException $e){
if($this->errors === true){
return $this->error($e->getMessage());
}else{
return false;
}
}
}else{
return false;
}
}
}