-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass.database.php
256 lines (222 loc) · 8.26 KB
/
class.database.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
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
<?PHP
class Database
{
// Singleton object. Leave $me alone.
private static $me;
public $db;
public $host;
public $name;
public $username;
public $password;
public $dieOnError;
public $queries;
public $result;
public $redirect = false;
// Singleton constructor
private function __construct($connect = false)
{
$Config = Config::getConfig();
$this->host = $Config->dbHost;
$this->name = $Config->dbName;
$this->username = $Config->dbUsername;
$this->password = $Config->dbPassword;
$this->dieOnError = $Config->dbDieOnError;
$this->db = false;
$this->queries = array();
if($connect === true)
$this->connect();
}
// Waiting (not so) patiently for 5.3.0...
public static function __callStatic($name, $args)
{
return self::$me->__call($name, $args);
}
// Get Singleton object
public static function getDatabase($connect = true)
{
if(is_null(self::$me))
self::$me = new Database($connect);
return self::$me;
}
// Do we have a valid database connection?
public function isConnected()
{
return is_resource($this->db) && get_resource_type($this->db) == 'mysql link';
}
// Do we have a valid database connection and have we selected a database?
public function databaseSelected()
{
if(!$this->isConnected()) return false;
$result = mysql_list_tables($this->name, $this->db);
return is_resource($result);
}
public function connect()
{
$this->db = mysql_connect($this->host, $this->username, $this->password) or $this->notify('Failed connecting to the database with the supplied connection details. Please check the details are correct and your MySQL user has permissions to access this database.<br/><br/>(host: '.$this->host.', user: '.$this->username.', pass: ********)');
if($this->db === false) return false;
mysql_select_db($this->name, $this->db) or $this->notify();
return $this->isConnected();
}
public function close()
{
if($this->db)
{
mysql_close($this->db);
}
$this->db = false;
$this->queries = array();
}
public function query($sql, $args_to_prepare = null, $exception_on_missing_args = true)
{
if(!$this->isConnected()) $this->connect();
// Allow for prepared arguments. Example:
// query("SELECT * FROM table WHERE id = :id", array('id' => $some_val));
if(is_array($args_to_prepare))
{
foreach($args_to_prepare as $name => $val)
{
$val = $this->quote($val);
$sql = str_replace(":$name", $val, $sql, $count);
if($exception_on_missing_args && (0 == $count))
throw new Exception(":$name was not found in prepared SQL query.");
}
}
$this->queries[] = $sql;
$this->result = mysql_query($sql, $this->db) or $this->notify();
return $this->result;
}
// Returns the number of rows.
// You can pass in nothing, a string, or a db result
public function numRows($arg = null)
{
$result = $this->resulter($arg);
return ($result !== false) ? mysql_num_rows($result) : false;
}
// Returns true / false if the result has one or more rows
public function hasRows($arg = null)
{
$result = $this->resulter($arg);
return is_resource($result) && (mysql_num_rows($result) > 0);
}
// Returns the number of rows affected by the previous operation
public function affectedRows()
{
if(!$this->isConnected()) return false;
return mysql_affected_rows($this->db);
}
// Returns the auto increment ID generated by the previous insert statement
public function insertId()
{
if(!$this->isConnected()) return false;
$id = mysql_insert_id($this->db);
if($id === 0 || $id === false)
return false;
else
return $id;
}
// Returns a single value.
// You can pass in nothing, a string, or a db result
public function getValue($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows($result) ? mysql_result($result, 0, 0) : false;
}
// Returns an array of the first value in each row.
// You can pass in nothing, a string, or a db result
public function getValues($arg = null)
{
$result = $this->resulter($arg);
if(!$this->hasRows($result)) return array();
$values = array();
mysql_data_seek($result, 0);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
$values[] = array_pop($row);
return $values;
}
// Returns the first row.
// You can pass in nothing, a string, or a db result
public function getRow($arg = null)
{
$result = $this->resulter($arg);
return $this->hasRows() ? mysql_fetch_array($result, MYSQL_ASSOC) : false;
}
// Returns an array of all the rows.
// You can pass in nothing, a string, or a db result
public function getRows($arg = null)
{
$result = $this->resulter($arg);
if(!$this->hasRows($result)) return array();
$rows = array();
mysql_data_seek($result, 0);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
$rows[] = $row;
return $rows;
}
// Escapes a value and wraps it in single quotes.
public function quote($var)
{
if(!$this->isConnected()) $this->connect();
return "'" . $this->escape($var) . "'";
}
// Escapes a value.
public function escape($var)
{
if(!$this->isConnected()) $this->connect();
return mysql_real_escape_string($var, $this->db);
}
public function numQueries()
{
return count($this->queries);
}
public function lastQuery()
{
if($this->numQueries() > 0)
return $this->queries[$this->numQueries() - 1];
else
return false;
}
private function notify($err_msg = null)
{
if($err_msg === null)
{
$err_msg = mysql_error($this->db);
}
error_log($err_msg);
if($this->dieOnError === true)
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Database Error:</strong><br/>$err_msg</p>";
if(strlen($this->lastQuery()))
{
echo "<p style='border:5px solid red;background-color:#fff;padding:12px;font-family: verdana, sans-serif;'><strong>Last Query:</strong><br/>" . $this->lastQuery() . "</p>";
}
//echo "<pre>";
//debug_print_backtrace();
//echo "</pre>";
exit;
}
if(is_string($this->redirect))
{
header("Location: {$this->redirect}");
exit;
}
}
// Takes nothing, a MySQL result, or a query string and returns
// the correspsonding MySQL result resource or false if none available.
private function resulter($arg = null)
{
if(is_null($arg) && is_resource($this->result))
return $this->result;
elseif(is_resource($arg))
return $arg;
elseif(is_string($arg))
{
$this->query($arg);
if(is_resource($this->result))
return $this->result;
else
return false;
}
else
return false;
}
}