-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass.dbMySQL.php
More file actions
executable file
·301 lines (277 loc) · 8.81 KB
/
class.dbMySQL.php
File metadata and controls
executable file
·301 lines (277 loc) · 8.81 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
<?PHP
////////////////////////////////////////////////////
//
// WeatherOffice
//
// https://github.com/BmdOnline/WeatherOffice
//
// Copyright (C) 09/2018
// BmdOnline,
//
// See COPYING for license info
//
////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Class DbMySQL handles data queries to MySQL / MariaDB
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class DbMySQL
{
private $errConnectDB = 'Connection Failed.<br>Host:<font color=red>$host</font><br>Error: %s\n';
private $errQuerySQL = 'Query Failed.<br>Query:<font color=red>%s</font><br>Error: %s<br>\n';
private $mysqli = null;
private $result = null;
private $results = null;
/**
* Constructor, open a new connection to the MySQL server
*
* @access public
* @param string $host Host address (or IP)
* @param string $username Username for database connection
* @param string $passwd Password for database connection
* @param string $dbname Database name
* @return void
*/
function __construct(string $host, string $username, string $passwd, string $dbname) {
//
// connect to the database
//
$this->mysqli = new mysqli($host, $username, $passwd, $dbname);
if ($this->mysqli->connect_errno) {
printf($this->errConnectDB, $this->mysqli->connect_error);
}
}
/**
* Destructor, Closes a previously opened database connection
*
* @access public
* @return void
*/
function __destruct() {
$this->close();
}
/**
* Returns the error code from last connect call
*
* @access public
* @return int Error code from last connect call
*/
function connected() {
return !($this->mysqli->connect_errno);
}
/**
* Closes a previously opened database connection
*
* @access public
* @return void
*/
function close() {
$this->free();
$this->freeall();
if ($this->mysqli) {
$this->mysqli->close();
$this->mysqli = null;
}
}
/**
* Frees the memory associated with a result
*
* @access public
* @return void
*/
function free() {
if ($this->result) {
$this->result->free();
$this->result = null;
}
}
/**
* Frees the memory associated with all stored results
*
* @access public
* @return void
*/
function freeall() {
if ($this->results) {
foreach($this->results as $result) {
if ($result) {
$result->free();
$result = null;
}
}
}
}
/**
* Store the memory associated with a result
*
* @access public
* @param string $alias Identify stored results
* @return void
*/
function store(string $alias) {
if ($this->result) {
if (isset($this->results[$alias])) {
$this->results[$alias]->free();
$this->results[$alias] = null;
}
$this->results[$alias] = $this->result;
$this->result = null;
}
}
/**
* Restore the memory associated with a result
*
* @access public
* @param string $alias Identify stored results
* @return void
*/
function restore(string $alias) {
if (isset($this->results[$alias])) {
$this->free();
$this->result = $this->results[$alias];
$this->results[$alias] = null;
}
}
/**
* Escapes special characters in a string for use in an SQL statement
*
* @access public
* @param string $escapestr The string to be escaped
* @return string Escaped string
*/
function escapeString (string $escapestr) {
return $this->mysqli->real_escape_string($escapestr);
}
/**
* Performs a query on the database
*
* @access public
* @param string $query The query, as a string
* @param boolean $volatile If true, drop mysqli_result object
* @return mixed true/false when not volatile, mysqli_result object otherwise
*/
function query(string $query, bool $volatile=false) {
$result = $this->mysqli->query($query);
if ($result) {
if ($volatile) {
return $result;
} else {
$this->result = $result;
return true;
}
} else {
printf($this->errQuerySQL, $query, $this->mysqli->error);
return false;
}
}
/**
* Check if MySQL table exists
*
* @access public
* @param string $table Table to check
* @return boolean true/false according to existing table
*/
function tableExists(string $table) {
$query = "SHOW TABLES LIKE '".$table."'";
$result = $this->query($query, true);
if ($result) {
$exists = $result->num_rows;
$result->free();
$result = null;
}
if ($exists) {
return true;
} else {
return false;
}
}
/**
* Perform query and return a single value
*
* @access public
* @param string $query The query, as a string
* @param string $index Which field to return
* @return mixed desired value
*/
function getValue(string $query, int $index=0) {
$result = $this->query($query, true);
if ($result) {
$datarow = $result->fetch_array();
$result->free();
$result = null;
return $datarow[$index];
} else {
return null;
}
}
/**
* Perform query and return its result, keep mysqli_result object for later use
*
* @access public
* @param string $query The query, as a string
* @return array associative array of strings representing the fetched row
*/
function getRows(string $query) {
if ($this->query($query)) {
$datarow = $this->result->fetch_assoc();
return $datarow;
} else {
return null;
}
}
/**
* Perform query and return its result, destroy mysqli_result object
*
* @access public
* @param string $query The query, as a string
* @return array associative array of strings representing the fetched row
*/
function getRow(string $query) {
$datarow = $this->getRows($query);
if ($datarow) {
$this->free();
return $datarow;
} else {
return null;
}
}
/**
* Return next result row as an associative array
*
* @access public
* @return array associative array of strings representing the next fetched row
*/
function getNextRow() {
$datarow = $this->result->fetch_assoc();
return $datarow;
}
/**
* Adjusts the result pointer to an arbitrary row in the result
*
* @access public
* @param int $position The field offset
* @return boolean true/false on success or failure
*/
function seekRow(int $position) {
if ($this->result) {
return $this->result->data_seek($position);
} else {
return false;
}
}
/**
* Gets the number of rows in a result
*
* @access public
* @return int number of rows in the result set
*/
function getRowsCount() {
if ($this->result) {
return $this->result->num_rows;
} else {
return 0;
}
}
}
?>