-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabase.php
More file actions
262 lines (246 loc) · 5.7 KB
/
Database.php
File metadata and controls
262 lines (246 loc) · 5.7 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
<?php
class Database
{
/**
* Database Host
*
* @var string
*/
private $host;
/**
* Database Username
*
* @var string
*/
private $user;
/**
* Database Password
*
* @var string
*/
private $pass;
/**
* Database Name
*
* @var string
*/
private $dbname;
/**
* Database Connection
*
* @var \PDO
*/
private $connection;
/**
* Database Connection Error
*
* @var string
*/
private $error;
/**
* Database PDO Statment
*
* @var mixed
*/
private $stmt;
/**
* Check if the database is connected
*
* @var bool
*/
private $dbconnected = false;
/**
* Controls the contents of the returned array
*
* @var int
*/
private $fetch_style = \PDO::FETCH_OBJ;
/**
* Database class constructor
*
* @return void
*/
public function __construct($config)
{
if ($config != []) {
$this->host = $config['DB_HOST'];
$this->user = $config['DB_USER'];
$this->pass = $config['DB_PASS'];
$this->dbname = $config['DB_NAME'];
}
$this->connect();
}
/**
* Create a connection between PHP and a database server.
*
* @return void
*/
private function connect()
{
// Set PDO Connection
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8';
$options = array(
\PDO::ATTR_PERSISTENT => true,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
);
// Create a new PDO instanace
try {
$this->connection = new \PDO($dsn, $this->user, $this->pass, $options);
$this->dbconnected = true;
} catch (\PDOException $e) {
$this->error = $e->getMessage();
}
}
/**
* Get the Error Message
*
* @return string
* Returns the error message generated from PDOException
*/
public function getError()
{
return $this->error;
}
/**
* Check if the class is connected to the database
*
* @return bool
* Returns true if the class is connected to the database or false otherwise
*/
public function isConnected()
{
return $this->dbconnected;
}
/**
* Prepares a statement for execution and returns a statement object
*
* @param string $query
* This must be a valid SQL statement for the target database server.
* @return void
*/
public function query($query)
{
$this->stmt = $this->connection->prepare($query);
}
/**
* Execute the prepared statement
*
* @return bool
* Returns true if the query is prepared query is executed successfully or false otherwise
*/
public function execute()
{
return $this->stmt->execute();
}
/**
* Execute an SQL statement and return the number of affected rows
*
* @param mixed $query
* The SQL statement to prepare and execute.
* @return mixed
* Returns the number of rows that were modified or deleted
*/
public function exec($query)
{
return $this->connection->exec($query);
}
/**
* Get the result set as an array of objects
*
* @return array
* Returns an array containing all of the remaining rows in the result set
*/
public function resultset()
{
$data = $this->stmt->fetchAll($this->fetch_style);
if (is_array($data)) {
return $data;
}
}
/**
* Get the record row count
*
* @return int
* Returns the number of rows
*/
public function rowCount()
{
$data = $this->stmt->rowCount();
if (is_int($data)) {
return $data;
}
}
/**
* Get a single record as an object
*
* @return object|bool
* Returns an object that contains the information from a single record or false otherwise
*/
public function single()
{
$data = $this->stmt->fetch($this->fetch_style);
if (is_object($data)) {
return $data;
} else {
return false;
}
}
/**
* Return the last inserted record id
*
* @return mixed
* Returns the last row id that was inserted into the database
*/
public function lastInsertId()
{
return $this->connection->lastInsertId();
}
/**
* Bind the values with the PDO statment
*
* @param string $param
* The query parameter you want bind to it
* @param mixed $value
* The value you want to bind with the parameter
* @param mixed $type
* The type of the parameter [optional]
* @return void
*/
public function bind($param, $value, $type = null)
{
if (is_null($type)) {
switch (true) {
case is_int($value):
$type = \PDO::PARAM_INT;
break;
case is_bool($value):
$type = \PDO::PARAM_BOOL;
break;
case is_null($value):
$type = \PDO::PARAM_NULL;
break;
default:
$type = \PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
/**
* Return the Database Name
*
* @return void
*/
public function returnDbName()
{
return $this->dbname;
}
/**
* Close the database connection
*
* @return void
*/
public function __destruct()
{
$this->connection = null;
}
}