forked from rcastera/MySQL-Wrapper-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass.Database.php
More file actions
317 lines (286 loc) · 8.46 KB
/
Class.Database.php
File metadata and controls
317 lines (286 loc) · 8.46 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* MySQL Wrapper Class in PHP5
* @author Richard Castera
* @link http://www.richardcastera.com/projects/mysql-wrapper-class-in-php5
* @see http://php.net/manual/en/book.mysql.php
* @license GNU LESSER GENERAL Public LICENSE
*/
class Database {
/**
* The link id to current connection.
* @var Variant
*/
private $linkId = NULL;
/**
* The result from the last query executed.
* @var Variant
*/
private $queryResult;
/**
* The first word in a query (ex: SELECT, INSERT, UPDATE, DELETE).
* @var String
*/
private $queryType;
/**
* The last insert id.
* @var Integer
*/
private $lastInsertId;
/**
* Constructor.
* @param String $host - The host to connect to.
* @param String $database - The database to connect to.
* @param String $username - The username of the db to connect to.
* @param String $password - The password of the db to connect to.
* @param Boolean $persistent - Is this a persistent connection or not.
*/
public function __construct($host = 'localhost', $database = '', $username = '', $password = '', $persistent = FALSE) {
if (empty($database) && empty($username) && empty($password)) {
trigger_error('Invalid parameter values to establish connection.', E_USER_ERROR);
}
else {
if (!$this->connect($host, $database, $username, $password, $persistent)) {
trigger_error('Could not establish a connection.', E_USER_ERROR);
}
}
}
/**
* Destructor - Disconnects from the database.
*/
public function __destruct() {
if ($this->linkId) {
mysql_close($this->linkId);
}
unset($this);
}
/**
* Establishes a connection to the database specified.
* @param String $host - The host to connect to.
* @param String $database - The database to connect to.
* @param String $username - The username of the db to connect to.
* @param String $password - The password of the db to connect to.
* @param Boolean $persistent - Is this a persistent connection or not.
* @return Boolean True if connected, False if not.
*/
private function connect($host, $database, $username, $password, $persistant) {
if (is_null($this->linkId)) {
if ($persistant) {
$this->linkId = mysql_pconnect($host, $username, $password, FALSE);
}
else {
$this->linkId = mysql_connect($host, $username, $password, FALSE);
}
// If there was an error establishing a connection, return false.
if (!is_resource($this->linkId)) {
return FALSE;
}
// If we couldn't select the database, return false.
if (!$this->selectDb($database)) {
trigger_error('Could not connect to database.', E_USER_ERROR);
return FALSE;
}
// Connection was a success.
else {
return TRUE;
}
}
else {
// Assume we already have a connection.
return TRUE;
}
}
/**
* Selects the database.
* @param String $database - The database to connect to.
* @return Boolean True for success, False if not.
*/
private function selectDb($database) {
// If there was an error selecting the database, return false.
if (!mysql_select_db($database, $this->linkId)) {
return FALSE;
}
else {
return TRUE;
}
}
/**
* Retrieves the last error.
* @return String The error text from the last MySQL function, or empty string if no error occurred.
*/
public function getError() {
return mysql_error($this->linkId);
}
/**
* Executes a command on the database.
* @param String $sql - the query to run.
* @return Mixed If True returns an array of rows. False if no rows.
*/
public function executeQuery($sql = '') {
// Check to see that the parameters are not empty.
if (!empty($sql)) {
// Execute the query.
$this->runQuery($sql);
return $this;
}
// Parameters are empty.
else {
trigger_error('You need to provide a query.', E_USER_ERROR);
}
}
/**
* Executes a sql query.
* @param String $query - The sql statement.
* @return Boolean True for success, False if not.
*/
private function runQuery($query = NULL) {
// Check to see if the sql statement variable is set.
if (!is_null($query)) {
// Determine the query type. (SELECT, UPDATE, INSERT, DELETE etc.)
$this->queryType = $this->getQueryType($query);
if (!$this->queryResult = mysql_query($query, $this->linkId)) {
trigger_error('Error in sql query.', E_USER_ERROR);
}
// Check if the query is an insert. If it is, get the id.
if ($this->queryType == 'INSERT') {
$this->lastInsertId = mysql_insert_id();
}
}
}
/**
* Gets the last insert id.
* @return Variant The ID generated for an AUTO_INCREMENT column or False.
*/
public function getLastInsertId() {
return $this->lastInsertId;
}
/**
* Free result memory.
* @return Returns True on success or False on failure.
*/
private function freeResult() {
return mysql_free_result($this->queryResult);
}
/**
* To determine the query type used in the query. ex: SELECT, INSERT. In order to run the getAffectedRows(); function below we need to determine the query type.
* @param String $query - The sql statement.
* @return String The first word in the query.
*/
private function getQueryType($query = '') {
$query = explode(' ', $query);
return strtoupper($query[0]);
}
/**
* Verifies that the last query executed successfully.
* @return True if the last query executed, False if not.
*/
public function querySucceeded() {
if (!$this->queryResult) {
return FALSE;
}
else {
return TRUE;
}
}
/**
* Gets the number of rows affected by the last query executed.
* @return Integer - The number of rows affected.
*/
public function getAffectedRows() {
if ($this->querySucceeded()) {
// Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set.
if (($this->queryType == 'SELECT') || ($this->queryType == 'SHOW')) {
return mysql_num_rows($this->queryResult);
}
else {
// To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
return mysql_affected_rows();
}
}
else {
return 0;
}
}
/**
* Verifies that the current INSERT sql call ran successfully.
* @return Mixed The last insert id if successful, false if not.
*/
public function wasInserted() {
if ($this->queryType == 'INSERT' && $this->querySucceeded()) {
return $this->getLastInsertId();
}
else {
return FALSE;
}
}
/**
* Verifies that the current UPDATE sql call ran successfully.
* @return Boolean True if successful, false if not.
*/
public function wasUpdated() {
if ($this->queryType == 'UPDATE' && $this->querySucceeded()) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Verifies that the current DELETE sql call ran successfully.
* @return Boolean True if successful, false if not.
*/
public function wasDeleted() {
if ($this->queryType == 'DELETE' && $this->querySucceeded()) {
return TRUE;
}
else {
return FALSE;
}
}
/**
* Return the result as an array.
* @return Mixed An array of rows if succcessful, false if not.
*/
public function asArray() {
// If the last query ran was unsuccessfull, then return false.
if (!$this->queryResult) {
return FALSE;
}
else {
if (!$this->getAffectedRows() == 0) {
$rows = array();
while ($row = mysql_fetch_assoc($this->queryResult)) {
array_push($rows, $row);
}
$this->freeResult();
return $rows;
}
else {
return FALSE;
}
}
}
/**
* Return the result as an object.
* @return Mixed An array of object rows if succcessful, false if not.
*/
public function asObject() {
// If the last query ran was unsuccessfull, then return false.
if (!$this->queryResult) {
return FALSE;
}
else {
if (!$this->getAffectedRows() == 0) {
$rows = array();
while ($row = mysql_fetch_object($this->queryResult)) {
array_push($rows, $row);
}
$this->freeResult();
return $rows;
}
else {
return FALSE;
}
}
}
}