|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mmanager\Extensions\Database; |
|
3
|
|
|
/********************************************************************** |
|
4
|
|
|
* Author: Juergen Bouché ([email protected]) |
|
5
|
|
|
* Web...: http://www.juergenbouche.de |
|
6
|
|
|
* Name..: ezSQL_mysqli |
|
7
|
|
|
* Desc..: mySQLi component (part of ezSQL database abstraction library) |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
/********************************************************************** |
|
12
|
|
|
* ezSQL error strings - mySQLi |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
global $ezsql_mysqli_str; |
|
16
|
|
|
|
|
17
|
|
|
$ezsql_mysqli_str = array( |
|
18
|
|
|
1 => 'Require $dbuser and $dbpassword to connect to a database server', |
|
19
|
|
|
2 => 'Error establishing mySQLi database connection. Correct user/password? Correct hostname? Database server running?', |
|
20
|
|
|
3 => 'Require $dbname to select a database', |
|
21
|
|
|
4 => 'mySQLi database connection is not active', |
|
22
|
|
|
5 => 'Unexpected error while trying to select database' |
|
23
|
|
|
); |
|
24
|
|
|
|
|
25
|
|
|
/********************************************************************** |
|
26
|
|
|
* ezSQL Database specific class - mySQLi |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
if ( ! function_exists('mysqli_connect')) die('<b>Fatal Error:</b> ezSQL_mysql requires mySQLi Lib to be compiled and or linked in to the PHP engine'); |
|
30
|
|
|
if ( ! class_exists('ezSQLcore')) die('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used'); |
|
31
|
|
|
|
|
32
|
|
|
class ezSQL_mysqli extends ezSQLcore |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
var $dbuser = false; |
|
36
|
|
|
var $dbpassword = false; |
|
37
|
|
|
var $dbname = false; |
|
38
|
|
|
var $dbhost = false; |
|
39
|
|
|
var $dbport = false; |
|
40
|
|
|
var $encoding = false; |
|
41
|
|
|
var $rows_affected = false; |
|
42
|
|
|
|
|
43
|
|
|
/********************************************************************** |
|
44
|
|
|
* Constructor - allow the user to perform a quick connect at the |
|
45
|
|
|
* same time as initialising the ezSQL_mysqli class |
|
46
|
|
|
*/ |
|
47
|
|
|
|
|
48
|
|
|
function __construct($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost', $encoding = '') |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$this->dbuser = $dbuser; |
|
|
|
|
|
|
51
|
|
|
$this->dbpassword = $dbpassword; |
|
|
|
|
|
|
52
|
|
|
$this->dbname = $dbname; |
|
|
|
|
|
|
53
|
|
|
list($this->dbhost, $this->dbport) = $this->get_host_port($dbhost, 3306); |
|
|
|
|
|
|
54
|
|
|
$this->encoding = $encoding; |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/********************************************************************** |
|
58
|
|
|
* Short hand way to connect to mySQL database server |
|
59
|
|
|
* and select a mySQL database at the same time |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
function quick_connect($dbuser = '', $dbpassword = '', $dbname = '', $dbhost = 'localhost', $dbport = '3306', $encoding = '') |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$return_val = false; |
|
65
|
|
|
if ( ! $this->connect($dbuser, $dbpassword, $dbhost, $dbport)); |
|
|
|
|
|
|
66
|
|
|
else if ( ! $this->select($dbname, $encoding)); |
|
67
|
|
|
else $return_val = true; |
|
68
|
|
|
return $return_val; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/********************************************************************** |
|
72
|
|
|
* Try to connect to mySQL database server |
|
73
|
|
|
*/ |
|
74
|
|
|
|
|
75
|
|
|
function connect($dbuser = '', $dbpassword = '', $dbhost = 'localhost', $dbport = false) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
global $ezsql_mysqli_str; $return_val = false; |
|
78
|
|
|
|
|
79
|
|
|
// Keep track of how long the DB takes to connect |
|
80
|
|
|
$this->timer_start('db_connect_time'); |
|
81
|
|
|
|
|
82
|
|
|
// If port not specified (new connection issued), get it |
|
83
|
|
|
if ( ! $dbport) { |
|
84
|
|
|
list($dbhost, $dbport) = $this->get_host_port($dbhost, 3306); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Must have a user and a password |
|
88
|
|
|
if ( ! $dbuser) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->register_error($ezsql_mysqli_str[1].' in '.__FILE__.' on line '.__LINE__); |
|
91
|
|
|
$this->show_errors ? trigger_error($ezsql_mysqli_str[1], E_USER_WARNING) : null; |
|
92
|
|
|
} |
|
93
|
|
|
// Try to establish the server database handle |
|
94
|
|
|
else |
|
95
|
|
|
{ |
|
96
|
|
|
$this->dbh = new \Mysqli($dbhost, $dbuser, $dbpassword, '', $dbport); |
|
|
|
|
|
|
97
|
|
|
// Check for connection problem |
|
98
|
|
|
if ($this->dbh->connect_errno) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->register_error($ezsql_mysqli_str[2].' in '.__FILE__.' on line '.__LINE__); |
|
101
|
|
|
$this->show_errors ? trigger_error($ezsql_mysqli_str[2], E_USER_WARNING) : null; |
|
102
|
|
|
} |
|
103
|
|
|
else |
|
104
|
|
|
{ |
|
105
|
|
|
$this->dbuser = $dbuser; |
|
|
|
|
|
|
106
|
|
|
$this->dbpassword = $dbpassword; |
|
|
|
|
|
|
107
|
|
|
$this->dbhost = $dbhost; |
|
108
|
|
|
$this->dbport = $dbport; |
|
|
|
|
|
|
109
|
|
|
$return_val = true; |
|
110
|
|
|
|
|
111
|
|
|
$this->conn_queries = 0; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $return_val; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/********************************************************************** |
|
119
|
|
|
* Try to select a mySQL database |
|
120
|
|
|
*/ |
|
121
|
|
|
|
|
122
|
|
|
function select($dbname = '', $encoding = '') |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
global $ezsql_mysqli_str; $return_val = false; |
|
125
|
|
|
|
|
126
|
|
|
// Must have a database name |
|
127
|
|
|
if ( ! $dbname) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->register_error($ezsql_mysqli_str[3].' in '.__FILE__.' on line '.__LINE__); |
|
130
|
|
|
$this->show_errors ? trigger_error($ezsql_mysqli_str[3], E_USER_WARNING) : null; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Must have an active database connection |
|
134
|
|
|
else if ( ! $this->dbh) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->register_error($ezsql_mysqli_str[4].' in '.__FILE__.' on line '.__LINE__); |
|
137
|
|
|
$this->show_errors ? trigger_error($ezsql_mysqli_str[4], E_USER_WARNING) : null; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
// Try to connect to the database |
|
141
|
|
|
else if ( ! @$this->dbh->select_db($dbname)) |
|
142
|
|
|
{ |
|
143
|
|
|
// Try to get error supplied by mysql if not use our own |
|
144
|
|
|
if ( ! $str = @$this->dbh->error) |
|
145
|
|
|
$str = $ezsql_mysqli_str[5]; |
|
146
|
|
|
|
|
147
|
|
|
$this->register_error($str.' in '.__FILE__.' on line '.__LINE__); |
|
148
|
|
|
$this->show_errors ? trigger_error($str, E_USER_WARNING) : null; |
|
149
|
|
|
} |
|
150
|
|
|
else |
|
151
|
|
|
{ |
|
152
|
|
|
$this->dbname = $dbname; |
|
|
|
|
|
|
153
|
|
|
if ($encoding != '') |
|
154
|
|
|
{ |
|
155
|
|
|
$encoding = strtolower(str_replace("-", "", $encoding)); |
|
156
|
|
|
$charsets = array(); |
|
157
|
|
|
$result = $this->dbh->query("SHOW CHARACTER SET"); |
|
158
|
|
|
while ($row = $result->fetch_array(MYSQLI_ASSOC)) |
|
159
|
|
|
{ |
|
160
|
|
|
$charsets[] = $row["Charset"]; |
|
161
|
|
|
} |
|
162
|
|
|
if (in_array($encoding, $charsets)) { |
|
163
|
|
|
$this->dbh->set_charset($encoding); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$return_val = true; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $return_val; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/********************************************************************** |
|
174
|
|
|
* Format a mySQL string correctly for safe mySQL insert |
|
175
|
|
|
* (no mater if magic quotes are on or not) |
|
176
|
|
|
*/ |
|
177
|
|
|
|
|
178
|
|
|
function escape($str) |
|
|
|
|
|
|
179
|
|
|
{ |
|
180
|
|
|
// If there is no existing database connection then try to connect |
|
181
|
|
|
if ( ! isset($this->dbh) || ! $this->dbh ) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->connect($this->dbuser, $this->dbpassword, $this->dbhost, $this->dbport); |
|
|
|
|
|
|
184
|
|
|
$this->select($this->dbname, $this->encoding); |
|
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
if ( get_magic_quotes_gpc() ) { |
|
188
|
|
|
$str = stripslashes($str); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
return $this->dbh->escape_string($str); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/********************************************************************** |
|
195
|
|
|
* Return mySQL specific system date syntax |
|
196
|
|
|
* i.e. Oracle: SYSDATE Mysql: NOW() |
|
197
|
|
|
*/ |
|
198
|
|
|
|
|
199
|
|
|
function sysdate() |
|
|
|
|
|
|
200
|
|
|
{ |
|
201
|
|
|
return 'NOW()'; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/********************************************************************** |
|
205
|
|
|
* Perform mySQL query and try to determine result value |
|
206
|
|
|
*/ |
|
207
|
|
|
|
|
208
|
|
|
function query($query) |
|
|
|
|
|
|
209
|
|
|
{ |
|
210
|
|
|
|
|
211
|
|
|
// This keeps the connection alive for very long running scripts |
|
212
|
|
|
if ($this->count(false) >= 500) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->disconnect(); |
|
215
|
|
|
$this->quick_connect($this->dbuser, $this->dbpassword, $this->dbname, $this->dbhost, $this->dbport, $this->encoding); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
// Initialise return |
|
219
|
|
|
$return_val = 0; |
|
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
// Flush cached values.. |
|
222
|
|
|
$this->flush(); |
|
223
|
|
|
|
|
224
|
|
|
// For reg expressions |
|
225
|
|
|
$query = trim($query); |
|
226
|
|
|
|
|
227
|
|
|
// Log how the function was called |
|
228
|
|
|
$this->func_call = "\$db->query(\"$query\")"; |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
// Keep track of the last query for debug.. |
|
231
|
|
|
$this->last_query = $query; |
|
232
|
|
|
|
|
233
|
|
|
// Count how many queries there have been |
|
234
|
|
|
$this->count(true, true); |
|
235
|
|
|
|
|
236
|
|
|
// Start timer |
|
237
|
|
|
$this->timer_start($this->num_queries); |
|
238
|
|
|
|
|
239
|
|
|
// Use core file cache function |
|
240
|
|
|
if ($cache = $this->get_cache($query)) |
|
241
|
|
|
{ |
|
242
|
|
|
// Keep tack of how long all queries have taken |
|
243
|
|
|
$this->timer_update_global($this->num_queries); |
|
244
|
|
|
|
|
245
|
|
|
// Trace all queries |
|
246
|
|
|
if ($this->use_trace_log) |
|
247
|
|
|
{ |
|
248
|
|
|
$this->trace_log[] = $this->debug(false); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $cache; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
// If there is no existing database connection then try to connect |
|
255
|
|
|
if ( ! isset($this->dbh) || ! $this->dbh) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->connect($this->dbuser, $this->dbpassword, $this->dbhost, $this->dbport); |
|
|
|
|
|
|
258
|
|
|
$this->select($this->dbname, $this->encoding); |
|
|
|
|
|
|
259
|
|
|
// No existing connection at this point means the server is unreachable |
|
260
|
|
|
if ( ! isset($this->dbh) || ! $this->dbh || $this->dbh->connect_errno) |
|
261
|
|
|
return false; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
// Perform the query via std mysql_query function.. |
|
265
|
|
|
$this->result = @$this->dbh->query($query); |
|
|
|
|
|
|
266
|
|
|
|
|
267
|
|
|
// If there is an error then take note of it.. |
|
268
|
|
|
if ($str = @$this->dbh->error) |
|
269
|
|
|
{ |
|
270
|
|
|
$this->register_error($str); |
|
271
|
|
|
$this->show_errors ? trigger_error($str, E_USER_WARNING) : null; |
|
272
|
|
|
return false; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
// Query was a Data Manipulation Query (insert, delete, update, replace, ...) |
|
276
|
|
|
if ( ! is_object($this->result)) |
|
277
|
|
|
{ |
|
278
|
|
|
$is_insert = true; |
|
279
|
|
|
$this->rows_affected = @$this->dbh->affected_rows; |
|
280
|
|
|
|
|
281
|
|
|
// Take note of the insert_id |
|
282
|
|
|
if (preg_match("/^(insert|replace)\s+/i", $query)) |
|
283
|
|
|
{ |
|
284
|
|
|
$this->insert_id = @$this->dbh->insert_id; |
|
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
// Return number fo rows affected |
|
288
|
|
|
$return_val = $this->rows_affected; |
|
289
|
|
|
} |
|
290
|
|
|
// Query was a Data Query Query (select, show, ...) |
|
291
|
|
|
else |
|
292
|
|
|
{ |
|
293
|
|
|
$is_insert = false; |
|
294
|
|
|
|
|
295
|
|
|
// Take note of column info |
|
296
|
|
|
$i = 0; |
|
297
|
|
|
while ($i < @$this->result->field_count) |
|
298
|
|
|
{ |
|
299
|
|
|
$this->col_info[$i] = @$this->result->fetch_field(); |
|
300
|
|
|
$i++; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
// Store Query Results |
|
304
|
|
|
$num_rows = 0; |
|
305
|
|
|
while ($row = @$this->result->fetch_object()) |
|
306
|
|
|
{ |
|
307
|
|
|
// Store relults as an objects within main array |
|
308
|
|
|
$this->last_result[$num_rows] = $row; |
|
|
|
|
|
|
309
|
|
|
$num_rows++; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
@$this->result->free_result(); |
|
|
|
|
|
|
313
|
|
|
|
|
314
|
|
|
// Log number of rows the query returned |
|
315
|
|
|
$this->num_rows = $num_rows; |
|
|
|
|
|
|
316
|
|
|
|
|
317
|
|
|
// Return number of rows selected |
|
318
|
|
|
$return_val = $this->num_rows; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
// disk caching of queries |
|
322
|
|
|
$this->store_cache($query, $is_insert); |
|
323
|
|
|
|
|
324
|
|
|
// If debug ALL queries |
|
325
|
|
|
$this->trace || $this->debug_all ? $this->debug() : null; |
|
326
|
|
|
|
|
327
|
|
|
// Keep tack of how long all queries have taken |
|
328
|
|
|
$this->timer_update_global($this->num_queries); |
|
329
|
|
|
|
|
330
|
|
|
// Trace all queries |
|
331
|
|
|
if ($this->use_trace_log) |
|
332
|
|
|
{ |
|
333
|
|
|
$this->trace_log[] = $this->debug(false); |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
return $return_val; |
|
337
|
|
|
|
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/********************************************************************** |
|
341
|
|
|
* Variables |
|
342
|
|
|
*/ |
|
343
|
|
|
private $s_query = ""; |
|
344
|
|
|
|
|
345
|
|
|
private $s_params; |
|
346
|
|
|
/********************************************************************** |
|
347
|
|
|
* set query |
|
348
|
|
|
*/ |
|
349
|
|
|
function set_query($query) |
|
|
|
|
|
|
350
|
|
|
{ |
|
351
|
|
|
$this->s_query = $query; |
|
352
|
|
|
$this->s_params = array(); |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/********************************************************************** |
|
356
|
|
|
* Special query to escape all parameters |
|
357
|
|
|
*/ |
|
358
|
|
|
function bind_param($parameter, $value) |
|
|
|
|
|
|
359
|
|
|
{ |
|
360
|
|
|
$value = $this->escape($value); |
|
361
|
|
|
$this->s_params[$parameter] = $value; |
|
362
|
|
|
return 1; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/********************************************************************** |
|
366
|
|
|
* Special query to escape all parameters |
|
367
|
|
|
*/ |
|
368
|
|
|
function execute() |
|
|
|
|
|
|
369
|
|
|
{ |
|
370
|
|
|
if ($this->s_query != '') |
|
371
|
|
|
{ |
|
372
|
|
|
$query = $this->s_query; |
|
373
|
|
|
|
|
374
|
|
|
if ( ! empty($this->s_params)) |
|
375
|
|
|
{ |
|
376
|
|
|
foreach ($this->s_params as $param => $value) |
|
377
|
|
|
{ |
|
378
|
|
|
$count = 0; |
|
379
|
|
|
$query = str_replace($param, $value, $query, $count); |
|
380
|
|
|
if ($count == 0) |
|
|
|
|
|
|
381
|
|
|
{ |
|
382
|
|
|
$str = $query.' no parameter was changed'; |
|
383
|
|
|
$this->register_error($str.' in '.__FILE__.' on line '.__LINE__); |
|
384
|
|
|
$this->show_errors ? trigger_error($str, E_USER_WARNING) : null; |
|
385
|
|
|
} |
|
386
|
|
|
} |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
$this->s_query = ""; |
|
390
|
|
|
$this->s_params = array(); |
|
391
|
|
|
|
|
392
|
|
|
return $this->query($query); |
|
393
|
|
|
} else |
|
394
|
|
|
{ |
|
395
|
|
|
return NULL; |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/********************************************************************** |
|
400
|
|
|
* Close the active mySQLi connection |
|
401
|
|
|
*/ |
|
402
|
|
|
|
|
403
|
|
|
function disconnect() |
|
|
|
|
|
|
404
|
|
|
{ |
|
405
|
|
|
$this->conn_queries = 0; |
|
406
|
|
|
@$this->dbh->close(); |
|
|
|
|
|
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
} |
|
410
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.