1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-31 03:40:37 +02:00

e_db_mysql: Replaced mysql with mysqli

Finally no longer using the mysql_* functions removed in PHP 7.0
This commit is contained in:
Nick Liu
2020-01-19 13:33:52 +01:00
parent 4a26ac5fd7
commit 8c528de191

View File

@@ -2,7 +2,7 @@
/* /*
* e107 website system * e107 website system
* *
* Copyright (C) 2008-2013 e107 Inc (e107.org) * Copyright (C) 2008-2020 e107 Inc (e107.org)
* Released under the terms and conditions of the * Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
* *
@@ -67,7 +67,7 @@ class e_db_mysql implements e_db
protected $mySQLport = 3306; protected $mySQLport = 3306;
public $mySQLPrefix; public $mySQLPrefix;
/** @var resource */ /** @var mysqli */
protected $mySQLaccess; protected $mySQLaccess;
public $mySQLresult; public $mySQLresult;
public $mySQLrows; public $mySQLrows;
@@ -183,13 +183,13 @@ class e_db_mysql implements e_db
$this->mySQLPrefix = $mySQLPrefix; $this->mySQLPrefix = $mySQLPrefix;
$this->mySQLerror = false; $this->mySQLerror = false;
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink)) if (!$this->mySQLaccess = @mysqli_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{ {
$this->mySQLlastErrText = mysql_error(); $this->mySQLlastErrText = mysqli_connect_error();
return 'e1'; return 'e1';
} }
$this->mySqlServerInfo = mysql_get_server_info(); // We always need this for db_Set_Charset() - so make generally available $this->mySqlServerInfo = mysqli_get_server_info($this->mySQLaccess); // We always need this for db_Set_Charset() - so make generally available
// Set utf8 connection? // Set utf8 connection?
//@TODO: simplify when yet undiscovered side-effects will be fixed //@TODO: simplify when yet undiscovered side-effects will be fixed
@@ -242,13 +242,13 @@ class e_db_mysql implements e_db
list($this->mySQLserver,$this->mySQLport) = explode(':',$mySQLserver,2); list($this->mySQLserver,$this->mySQLport) = explode(':',$mySQLserver,2);
} }
if (!$this->mySQLaccess = @mysql_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink)) if (!$this->mySQLaccess = @mysqli_connect($this->mySQLserver, $this->mySQLuser, $this->mySQLpassword, $newLink))
{ {
$this->mySQLlastErrText = mysql_error(); $this->mySQLlastErrText = mysqli_connect_error();
return false; return false;
} }
$this->mySqlServerInfo = mysql_get_server_info(); $this->mySqlServerInfo = mysqli_get_server_info($this->mySQLaccess);
$this->db_Set_Charset(); $this->db_Set_Charset();
$this->setSQLMode(); $this->setSQLMode();
@@ -289,7 +289,7 @@ class e_db_mysql implements e_db
return true; return true;
} }
if (!@mysql_select_db($database, $this->mySQLaccess)) if (!@mysqli_select_db($this->mySQLaccess, $database))
{ {
return false; return false;
} }
@@ -361,8 +361,8 @@ class e_db_mysql implements e_db
* *
* If a SELECT query includes SQL_CALC_FOUND_ROWS, the value of FOUND_ROWS() is retrieved and stored in $this->total_results * If a SELECT query includes SQL_CALC_FOUND_ROWS, the value of FOUND_ROWS() is retrieved and stored in $this->total_results
* @param string|array $query * @param string|array $query
* @param object $rli * @param mysqli $rli Your own mysqli connection instead of the one in this object
* @return boolean|resource - as mysql_query() function. * @return boolean|mysqli_result - as mysqli_query() function.
* FALSE indicates an error * FALSE indicates an error
* For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource * For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource
* TRUE indicates success in other cases * TRUE indicates success in other cases
@@ -392,9 +392,9 @@ class e_db_mysql implements e_db
$b = microtime(); $b = microtime();
$sQryRes = is_null($rli) ? @mysql_query($query, $this->mySQLaccess) : @mysql_query($query, $rli); $sQryRes = is_null($rli) ? @mysqli_query($this->mySQLaccess, $query) : @mysqli_query($rli, $query);
$this->mySQLlastErrNum = mysql_errno(); $this->mySQLlastErrNum = mysqli_errno($this->mySQLaccess);
$this->mySQLlastErrText = mysql_error(); $this->mySQLlastErrText = mysqli_error($this->mySQLaccess);
$e = microtime(); $e = microtime();
@@ -416,8 +416,8 @@ class e_db_mysql implements e_db
if (!is_array($query) && (strpos($query,'EXPLAIN') !==0) && (strpos($query,'SQL_CALC_FOUND_ROWS') !== false) && (strpos($query,'SELECT') !== false)) if (!is_array($query) && (strpos($query,'EXPLAIN') !==0) && (strpos($query,'SQL_CALC_FOUND_ROWS') !== false) && (strpos($query,'SELECT') !== false))
{ {
$fr = mysql_query('SELECT FOUND_ROWS()', $this->mySQLaccess); $fr = mysqli_query($this->mySQLaccess, 'SELECT FOUND_ROWS()');
$rc = mysql_fetch_array($fr); $rc = mysqli_fetch_array($fr);
$this->total_results = (int)$rc['FOUND_ROWS()']; $this->total_results = (int)$rc['FOUND_ROWS()'];
} }
@@ -587,7 +587,7 @@ class e_db_mysql implements e_db
} }
/** /**
* Perform a mysql_query() using the arguments suplied by calling db::db_Query()<br /> * Perform a mysqli_query() using the arguments suplied by calling db::db_Query()<br />
* <br /> * <br />
* If you need more requests think to call the class.<br /> * If you need more requests think to call the class.<br />
* <br /> * <br />
@@ -786,7 +786,7 @@ class e_db_mysql implements e_db
{ {
$result = false; // ie. there was an error. $result = false; // ie. there was an error.
$this->mySQLresult = mysql_affected_rows($this->mySQLaccess); $this->mySQLresult = mysqli_affected_rows($this->mySQLaccess);
if($this->mySQLresult === 1 ) // insert. if($this->mySQLresult === 1 ) // insert.
{ {
@@ -813,10 +813,10 @@ class e_db_mysql implements e_db
{ {
if(true === $REPLACE) if(true === $REPLACE)
{ {
$tmp = mysql_affected_rows($this->mySQLaccess); $tmp = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Replace'); $this->dbError('db_Replace');
// $tmp == -1 (error), $tmp == 0 (not modified), $tmp == 1 (added), greater (replaced) // $tmp == -1 (error), $tmp == 0 (not modified), $tmp == 1 (added), greater (replaced)
if ($tmp == -1) { return false; } // mysql_affected_rows error if ($tmp == -1) { return false; } // mysqli_affected_rows error
return $tmp; return $tmp;
} }
@@ -835,7 +835,7 @@ class e_db_mysql implements e_db
public function lastInsertId() public function lastInsertId()
{ {
$tmp = mysql_insert_id($this->mySQLaccess); $tmp = mysqli_insert_id($this->mySQLaccess);
return ($tmp) ? $tmp : true; // return true even if table doesn't have auto-increment. return ($tmp) ? $tmp : true; // return true even if table doesn't have auto-increment.
} }
@@ -850,18 +850,18 @@ class e_db_mysql implements e_db
} }
/** /**
* @param resource $result * @param mysqli_result $result
* @return false|int * @return false|int
*/ */
public function rowCount($result=null) public function rowCount($result=null)
{ {
if (!is_resource($result)) if (!($result instanceof mysqli_result))
{ {
$result = $this->mySQLresult; $result = $this->mySQLresult;
} }
if (is_resource($result)) if ($result instanceof mysqli_result)
{ {
$this->mySQLrows = mysql_num_rows($result); $this->mySQLrows = mysqli_num_rows($result);
} }
$this->dbError('db_Rows'); $this->dbError('db_Rows');
return $this->mySQLrows; return $this->mySQLrows;
@@ -986,10 +986,10 @@ class e_db_mysql implements e_db
if ($result !==false) if ($result !==false)
{ {
$result = mysql_affected_rows($this->mySQLaccess); $result = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Update'); $this->dbError('db_Update');
if ($result === -1) { return false; } // Error return from mysql_affected_rows if ($result === -1) { return false; } // Error return from mysqli_affected_rows
return $result; return $result;
} }
else else
@@ -1125,8 +1125,8 @@ class e_db_mysql implements e_db
} }
if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.$this->mySQLPrefix.$table.' SET '.$new_data.$vars.' '.$arg, NULL, 'db_UpdateArray', $debug, $log_type, $log_remark)) if ($result = $this->mySQLresult = $this->db_Query('UPDATE '.$this->mySQLPrefix.$table.' SET '.$new_data.$vars.' '.$arg, NULL, 'db_UpdateArray', $debug, $log_type, $log_remark))
{ {
$result = mysql_affected_rows($this->mySQLaccess); $result = mysqli_affected_rows($this->mySQLaccess);
if ($result == -1) return FALSE; // Error return from mysql_affected_rows if ($result == -1) return FALSE; // Error return from mysqli_affected_rows
return $result; return $result;
} }
else else
@@ -1151,7 +1151,7 @@ class e_db_mysql implements e_db
/** /**
* @param string $type assoc|num|both * @param string $type assoc|num|both
* @return array|bool MySQL row * @return array|bool MySQL row
* @desc Fetch an array containing row data (see PHP's mysql_fetch_array() docs)<br /> * @desc Fetch an array containing row data (see PHP's mysqli_fetch_array() docs)<br />
* @example * @example
* Example :<br /> * Example :<br />
* <code>while($row = $sql->fetch()){ * <code>while($row = $sql->fetch()){
@@ -1188,7 +1188,7 @@ class e_db_mysql implements e_db
if($this->mySQLresult) if($this->mySQLresult)
{ {
$row = @mysql_fetch_array($this->mySQLresult, $type); $row = @mysqli_fetch_array($this->mySQLresult, $type);
e107::getSingleton('e107_traffic')->Bump('db_Fetch', $b); e107::getSingleton('e107_traffic')->Bump('db_Fetch', $b);
if ($row) if ($row)
{ {
@@ -1233,7 +1233,7 @@ class e_db_mysql implements e_db
$query=$table; $query=$table;
if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark)) if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark))
{ {
$rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult); $rows = $this->mySQLrows = @mysqli_fetch_array($this->mySQLresult);
$this->dbError('db_Count'); $this->dbError('db_Count');
return (int) $rows['COUNT(*)']; return (int) $rows['COUNT(*)'];
} }
@@ -1253,7 +1253,7 @@ class e_db_mysql implements e_db
$query='SELECT COUNT'.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg; $query='SELECT COUNT'.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg;
if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark)) if ($this->mySQLresult = $this->db_Query($query, NULL, 'db_Count', $debug, $log_type, $log_remark))
{ {
$rows = $this->mySQLrows = @mysql_fetch_array($this->mySQLresult); $rows = $this->mySQLrows = @mysqli_fetch_array($this->mySQLresult);
$this->dbError('db_Count'); $this->dbError('db_Count');
return (int) $rows[0]; return (int) $rows[0];
} }
@@ -1290,8 +1290,8 @@ class e_db_mysql implements e_db
{ {
$this->provide_mySQLaccess(); $this->provide_mySQLaccess();
e107::getSingleton('e107_traffic')->BumpWho('db Close', 1); e107::getSingleton('e107_traffic')->BumpWho('db Close', 1);
$this->mySQLaccess = NULL; // correct way to do it when using shared links. $result = mysqli_close($this->mySQLaccess);
$this->dbError('dbClose'); if ($result === false) $this->dbError('dbClose');
} }
@@ -1328,7 +1328,7 @@ class e_db_mysql implements e_db
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark)) if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{ {
// return the number of records deleted instead of an object // return the number of records deleted instead of an object
$this->mySQLrows = mysql_affected_rows($this->mySQLaccess); $this->mySQLrows = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete'); $this->dbError('db_Delete');
return $this->mySQLrows; return $this->mySQLrows;
} }
@@ -1342,7 +1342,7 @@ class e_db_mysql implements e_db
{ {
if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark)) if ($result = $this->mySQLresult = $this->db_Query('DELETE FROM '.$this->mySQLPrefix.$table.' WHERE '.$arg, NULL, 'db_Delete', $debug, $log_type, $log_remark))
{ {
$this->mySQLrows = mysql_affected_rows($this->mySQLaccess); $this->mySQLrows = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete'); $this->dbError('db_Delete');
return $this->mySQLrows; return $this->mySQLrows;
} }
@@ -1426,8 +1426,8 @@ class e_db_mysql implements e_db
elseif ($this->mySQLresult === TRUE) elseif ($this->mySQLresult === TRUE)
{ // Successful query which may return a row count (because it operated on a number of rows without returning a result set) { // Successful query which may return a row count (because it operated on a number of rows without returning a result set)
if(preg_match('#^(DELETE|INSERT|REPLACE|UPDATE)#',$query, $matches)) if(preg_match('#^(DELETE|INSERT|REPLACE|UPDATE)#',$query, $matches))
{ // Need to check mysql_affected_rows() - to return number of rows actually updated { // Need to check mysqli_affected_rows() - to return number of rows actually updated
$tmp = mysql_affected_rows($this->mySQLaccess); $tmp = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Select_gen'); $this->dbError('db_Select_gen');
return $tmp; return $tmp;
} }
@@ -1462,50 +1462,6 @@ class e_db_mysql implements e_db
return " ".$this->mySQLPrefix.$table.substr($matches[0],-1); return " ".$this->mySQLPrefix.$table.substr($matches[0],-1);
} }
/**
* @return unknown
* @param unknown $offset
* @desc Enter description here...
* @access private
*/
/* Function not used
function db_Fieldname($offset)
{
$result = @mysql_field_name($this->mySQLresult, $offset);
return $result;
}
*/
/**
* @return unknown
* @desc Enter description here...
* @access private
*/
/*
function db_Field_info()
{
$result = @mysql_fetch_field($this->mySQLresult);
return $result;
}
*/
/**
* @return unknown
* @desc Enter description here...
* @access private
*/
/* Function not used
function db_Num_fields()
{
$result = @mysql_num_fields($this->mySQLresult);
return $result;
}
*/
/** /**
* Check for the existence of a matching language table when multi-language tables are active. * Check for the existence of a matching language table when multi-language tables are active.
* @param string|array $table Name of table, without the prefix. or an array of table names. * @param string|array $table Name of table, without the prefix. or an array of table names.
@@ -1899,7 +1855,7 @@ class e_db_mysql implements e_db
function columnCount() function columnCount()
{ {
return mysql_num_fields($this->mySQLresult); return mysqli_num_fields($this->mySQLresult);
} }
/** /**
@@ -2020,7 +1976,7 @@ class e_db_mysql implements e_db
/** /**
* A pointer to mysql_real_escape_string() - see http://www.php.net/mysql_real_escape_string * A pointer to mysqli_real_escape_string() - see https://www.php.net/manual/en/mysqli.real-escape-string.php
* *
* @param string $data * @param string $data
* @return string * @return string
@@ -2034,7 +1990,7 @@ class e_db_mysql implements e_db
$this->provide_mySQLaccess(); $this->provide_mySQLaccess();
return mysql_real_escape_string($data,$this->mySQLaccess); return mysqli_real_escape_string($this->mySQLaccess, $data);
} }
@@ -2367,13 +2323,13 @@ class e_db_mysql implements e_db
*/ */
function dbError($from) function dbError($from)
{ {
$this->mySQLlastErrNum = mysql_errno(); $this->mySQLlastErrNum = mysqli_errno($this->mySQLaccess);
$this->mySQLlastErrText = ''; $this->mySQLlastErrText = '';
if ($this->mySQLlastErrNum == 0) if ($this->mySQLlastErrNum == 0)
{ {
return ''; return '';
} }
$this->mySQLlastErrText = mysql_error(); // Get the error text. $this->mySQLlastErrText = mysqli_error($this->mySQLaccess); // Get the error text.
if ($this->mySQLerror == TRUE) if ($this->mySQLerror == TRUE)
{ {
message_handler('ADMIN_MESSAGE', '<b>mySQL Error!</b> Function: '.$from.'. ['.$this->mySQLlastErrNum.' - '.$this->mySQLlastErrText.']', __LINE__, __FILE__); message_handler('ADMIN_MESSAGE', '<b>mySQL Error!</b> Function: '.$from.'. ['.$this->mySQLlastErrNum.' - '.$this->mySQLlastErrText.']', __LINE__, __FILE__);
@@ -2439,7 +2395,7 @@ class e_db_mysql implements e_db
{ {
if ( ! $debug) if ( ! $debug)
{ {
@mysql_query("SET NAMES `$charset`"); @mysqli_query($this->mySQLaccess, "SET NAMES `$charset`");
} }
else else
{ {