Skip to content

Commit 0e0d06a

Browse files
committed
remove direct pubic access to most prepare functions, rename for consistency, overall performance edits
1 parent 36433e9 commit 0e0d06a

File tree

8 files changed

+281
-294
lines changed

8 files changed

+281
-294
lines changed

lib/ez_sql_mysqli.php

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ class ezSQL_mysqli extends ezSQLcore
9393
* Default is empty string
9494
*/
9595
public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost', $charset='') {
96-
if ( ! function_exists ('mysqli_connect') ) {
96+
if ( ! \function_exists ('mysqli_connect') ) {
9797
throw new Exception('<b>Fatal Error:</b> ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine');
9898
}
99-
if ( ! class_exists ('ezSQLcore') ) {
99+
if ( ! \class_exists ('ezSQLcore') ) {
100100
throw new Exception('<b>Fatal Error:</b> ezSQL_mysql requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used');
101101
}
102102

@@ -107,7 +107,7 @@ public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='loc
107107
$this->_dbname = $dbname;
108108
$this->_dbhost = $dbhost;
109109
if ( ! empty($charset) ) {
110-
$this->_charset = strtolower(str_replace('-', '', $charset));
110+
$this->_charset = \strtolower(\str_replace('-', '', $charset));
111111
}
112112

113113
$GLOBALS['db_mysql'] = $this;
@@ -156,13 +156,13 @@ public function connect($dbuser='', $dbpassword='', $dbhost='localhost', $charse
156156
// Must have a user and a password
157157
if ( empty($this->_dbuser) ) {
158158
$this->register_error($this->ezsql_mysql_str[1] . ' in ' . __FILE__ . ' on line ' . __LINE__);
159-
$this->show_errors ? trigger_error($this->ezsql_mysql_str[1], E_USER_WARNING) : null;
160-
} else if ( ! $this->dbh = mysqli_connect($this->_dbhost, $this->_dbuser, $this->_dbpassword, $this->_dbname) ) {
159+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[1], \E_USER_WARNING) : null;
160+
} else if ( ! $this->dbh = \mysqli_connect($this->_dbhost, $this->_dbuser, $this->_dbpassword, $this->_dbname) ) {
161161
// Try to establish the server database handle
162162
$this->register_error($this->ezsql_mysql_str[2] . ' in ' . __FILE__ . ' on line ' . __LINE__);
163-
$this->show_errors ? trigger_error($this->ezsql_mysql_str[2], E_USER_WARNING) : null;
163+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[2], \E_USER_WARNING) : null;
164164
} else {
165-
mysqli_set_charset($this->dbh, $this->_charset);
165+
\mysqli_set_charset($this->dbh, $this->_charset);
166166
$this->_connected = true;
167167
}
168168

@@ -180,37 +180,37 @@ public function select($dbname='', $charset='') {
180180
if ( ! $dbname ) {
181181
// Must have a database name
182182
$this->register_error($this->ezsql_mysql_str[3] . ' in ' . __FILE__ . ' on line ' . __LINE__);
183-
$this->show_errors ? trigger_error($this->ezsql_mysql_str[3], E_USER_WARNING) : null;
183+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[3], \E_USER_WARNING) : null;
184184
return false;
185185
} else if ( ! $this->dbh ) {
186186
// Must have an active database connection
187187
$this->register_error($this->ezsql_mysql_str[4] . ' in ' . __FILE__ . ' on line ' . __LINE__);
188-
$this->show_errors ? trigger_error($this->ezsql_mysql_str[4], E_USER_WARNING) : null;
188+
$this->show_errors ? \trigger_error($this->ezsql_mysql_str[4], \E_USER_WARNING) : null;
189189
return false;
190-
} else if ( !mysqli_select_db($this->dbh, $dbname) ) {
190+
} else if ( !\mysqli_select_db($this->dbh, $dbname) ) {
191191
// Try to connect to the database
192192
// Try to get error supplied by mysql if not use our own
193-
if ( !$str = mysqli_error($this->dbh)) {
193+
if ( !$str = \mysqli_error($this->dbh)) {
194194
$str = $this->ezsql_mysql_str[5];
195195
}
196196

197197
$this->register_error($str . ' in ' .__FILE__ . ' on line ' . __LINE__);
198-
$this->show_errors ? trigger_error($str, E_USER_WARNING) : null;
198+
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
199199
return false;
200200
} else {
201201
$this->_dbname = $dbname;
202202
if ( $charset == '') {
203203
$charset = $this->_charset;
204204
}
205205
if ( $charset != '' ) {
206-
$encoding = strtolower(str_replace('-', '', $charset));
206+
$encoding = \strtolower(\str_replace('-', '', $charset));
207207
$charsets = array();
208-
$recordset = mysqli_query($this->dbh, 'SHOW CHARACTER SET');
209-
while ( $row = mysqli_fetch_array($recordset, MYSQLI_ASSOC) ) {
208+
$recordset = \mysqli_query($this->dbh, 'SHOW CHARACTER SET');
209+
while ( $row = \mysqli_fetch_array($recordset, \MYSQLI_ASSOC) ) {
210210
$charsets[] = $row['Charset'];
211211
}
212-
if ( in_array($charset, $charsets) ) {
213-
mysqli_query($this->dbh, 'SET NAMES \'' . $encoding . '\'');
212+
if ( \in_array($charset, $charsets) ) {
213+
\mysqli_query($this->dbh, 'SET NAMES \'' . $encoding . '\'');
214214
}
215215
}
216216
$this->_connected = true;
@@ -227,7 +227,7 @@ public function select($dbname='', $charset='') {
227227
* @return string
228228
*/
229229
public function escape($str) {
230-
return mysqli_real_escape_string($this->dbh, stripslashes($str));
230+
return \mysqli_real_escape_string($this->dbh, \stripslashes($str));
231231
} // escape
232232

233233
/**
@@ -247,11 +247,11 @@ function fetch_prepared_result(&$stmt, $query) {
247247
$stmt->store_result();
248248
$variables = array();
249249
$is_insert = false;
250-
if ( preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
251-
$this->_affectedRows = mysqli_stmt_affected_rows($stmt);
250+
if ( \preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
251+
$this->_affectedRows = \mysqli_stmt_affected_rows($stmt);
252252

253253
// Take note of the insert_id
254-
if ( preg_match("/^(insert|replace)\s+/i", $query) ){
254+
if ( \preg_match("/^(insert|replace)\s+/i", $query) ){
255255
$this->insert_id = $stmt->insert_id;
256256
}
257257
} else {
@@ -263,7 +263,7 @@ function fetch_prepared_result(&$stmt, $query) {
263263
$variables[] = &$this->col_info[$field->name]; // pass by reference
264264

265265
// Binds variables to a prepared statement for result storage
266-
call_user_func_array([$stmt, 'bind_result'], $variables);
266+
\call_user_func_array([$stmt, 'bind_result'], $variables);
267267

268268
$i=0;
269269
// Store Query Results
@@ -279,7 +279,7 @@ function fetch_prepared_result(&$stmt, $query) {
279279
if ( $str = $stmt->error ) {
280280
$is_insert = true;
281281
$this->register_error($str);
282-
$this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
282+
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
283283

284284
// If debug ALL queries
285285
$this->trace || $this->debug_all ? $this->debug() : null ;
@@ -311,31 +311,32 @@ public function query_prepared($query, array $args)
311311
{
312312
$stmt = $this->dbh->prepare($query);
313313
$params = [];
314-
$types = array_reduce($args,
315-
function ($string, &$arg) use (&$params) {
316-
$params[] = &$arg;
317-
if (is_float($arg))
318-
$string .= 'd';
319-
elseif (is_integer($arg))
320-
$string .= 'i';
321-
elseif (is_string($arg))
322-
$string .= 's';
323-
else
324-
$string .= 'b';
325-
return $string;
326-
}, '');
314+
$types = \array_reduce($args,
315+
function ($string, &$arg) use (&$params) {
316+
$params[] = &$arg;
317+
if (\is_float($arg))
318+
$string .= 'd';
319+
elseif (\is_integer($arg))
320+
$string .= 'i';
321+
elseif (\is_string($arg))
322+
$string .= 's';
323+
else
324+
$string .= 'b';
325+
return $string;
326+
}, ''
327+
);
327328

328-
array_unshift($params, $types);
329+
\array_unshift($params, $types);
329330

330-
call_user_func_array([$stmt, 'bind_param'], $params);
331+
\call_user_func_array([$stmt, 'bind_param'], $params);
331332

332333
$result = ($stmt->execute()) ? $this->fetch_prepared_result($stmt, $query) : false;
333334

334335
// free and closes a prepared statement
335336
$stmt->free_result();
336337
$stmt->close();
337338

338-
$this->clearParameters();
339+
$this->clearPrepare();
339340

340341
return $result;
341342
}
@@ -349,10 +350,10 @@ function ($string, &$arg) use (&$params) {
349350
public function query($query, $use_prepare=false) {
350351
$param = [];
351352
if ($use_prepare)
352-
$param = $this->getParameters();
353+
$param = $this->prepareValues();
353354

354355
// check for ezQuery placeholder tag and replace tags with proper prepare tag
355-
$query = str_replace(_TAG, '?', $query);
356+
$query = \str_replace(\_TAG, '?', $query);
356357

357358
// Initialize return
358359
$return_val = 0;
@@ -361,7 +362,7 @@ public function query($query, $use_prepare=false) {
361362
$this->flush();
362363

363364
// For reg expressions
364-
$query = trim($query);
365+
$query = \trim($query);
365366

366367
// Log how the function was called
367368
$this->log_query("\$db->query(\"$query\")");
@@ -384,16 +385,16 @@ public function query($query, $use_prepare=false) {
384385
}
385386

386387
// Perform the query via std mysql_query function..
387-
if (!empty($param) && is_array($param) && ($this->isPrepareActive()))
388+
if (!empty($param) && \is_array($param) && ($this->isPrepareActive()))
388389
return $this->query_prepared($query, $param);
389390
else
390-
$this->_result = mysqli_query($this->dbh, $query);
391+
$this->_result = \mysqli_query($this->dbh, $query);
391392

392393
// If there is an error then take note of it..
393-
if ( $str = mysqli_error($this->dbh) ) {
394+
if ( $str = \mysqli_error($this->dbh) ) {
394395
$is_insert = true;
395396
$this->register_error($str);
396-
$this->show_errors ? trigger_error($str,E_USER_WARNING) : null;
397+
$this->show_errors ? \trigger_error($str, \E_USER_WARNING) : null;
397398

398399
// If debug ALL queries
399400
$this->trace || $this->debug_all ? $this->debug() : null ;
@@ -402,36 +403,36 @@ public function query($query, $use_prepare=false) {
402403

403404
// Query was an insert, delete, update, replace
404405
$is_insert = false;
405-
if ( preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
406-
$this->_affectedRows = mysqli_affected_rows($this->dbh);
406+
if ( \preg_match("/^(insert|delete|update|replace)\s+/i", $query) ) {
407+
$this->_affectedRows = \mysqli_affected_rows($this->dbh);
407408

408409
// Take note of the insert_id
409-
if ( preg_match("/^(insert|replace)\s+/i", $query) ) {
410-
$this->insert_id = mysqli_insert_id($this->dbh);
410+
if ( \preg_match("/^(insert|replace)\s+/i", $query) ) {
411+
$this->insert_id = \mysqli_insert_id($this->dbh);
411412
}
412413

413414
// Return number of rows affected
414415
$return_val = $this->_affectedRows;
415416
} else {
416-
if ( !is_numeric($this->_result) && !is_bool($this->_result)) {
417+
if ( !\is_numeric($this->_result) && !\is_bool($this->_result)) {
417418
// Query was a select
418419

419420
// Take note of column info
420421
$i=0;
421-
while ($i < mysqli_num_fields($this->_result)) {
422-
$this->col_info[$i] = mysqli_fetch_field($this->_result);
422+
while ($i < \mysqli_num_fields($this->_result)) {
423+
$this->col_info[$i] = \mysqli_fetch_field($this->_result);
423424
$i++;
424425
}
425426

426427
// Store Query Results
427428
$num_rows=0;
428-
while ( $row = mysqli_fetch_object($this->_result) ) {
429+
while ( $row = \mysqli_fetch_object($this->_result) ) {
429430
// Store results as an objects within main array
430431
$this->last_result[$num_rows] = $row;
431432
$num_rows++;
432433
}
433434

434-
mysqli_free_result($this->_result);
435+
\mysqli_free_result($this->_result);
435436

436437
// Log number of rows the query returned
437438
$this->num_rows = $num_rows;
@@ -455,7 +456,7 @@ public function query($query, $use_prepare=false) {
455456
*/
456457
public function disconnect() {
457458
if ( $this->dbh ) {
458-
mysqli_close($this->dbh);
459+
\mysqli_close($this->dbh);
459460
$this->_connected = false;
460461
}
461462

@@ -486,7 +487,7 @@ public function getCharset() {
486487
* @return int
487488
*/
488489
public function getInsertId() {
489-
return mysqli_insert_id($this->dbh);
490+
return \mysqli_insert_id($this->dbh);
490491
} // getInsertId
491492

492493
} // ezSQL_mysqli

0 commit comments

Comments
 (0)