Skip to content

Commit

Permalink
e_db_mysql: Replaced mysql with mysqli
Browse files Browse the repository at this point in the history
Finally no longer using the mysql_* functions removed in PHP 7.0
  • Loading branch information
Deltik committed Jan 19, 2020
1 parent 4a26ac5 commit 8c528de
Showing 1 changed file with 45 additions and 89 deletions.
134 changes: 45 additions & 89 deletions e107_handlers/mysql_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
* 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
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
Expand Down Expand Up @@ -67,7 +67,7 @@ class e_db_mysql implements e_db
protected $mySQLport = 3306;
public $mySQLPrefix;

/** @var resource */
/** @var mysqli */
protected $mySQLaccess;
public $mySQLresult;
public $mySQLrows;
Expand Down Expand Up @@ -183,13 +183,13 @@ public function db_Connect($mySQLserver, $mySQLuser, $mySQLpassword, $mySQLdefau
$this->mySQLPrefix = $mySQLPrefix;
$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';
}

$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?
//@TODO: simplify when yet undiscovered side-effects will be fixed
Expand Down Expand Up @@ -242,13 +242,13 @@ public function connect($mySQLserver, $mySQLuser, $mySQLpassword, $newLink = fal
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;
}

$this->mySqlServerInfo = mysql_get_server_info();
$this->mySqlServerInfo = mysqli_get_server_info($this->mySQLaccess);

$this->db_Set_Charset();
$this->setSQLMode();
Expand Down Expand Up @@ -289,7 +289,7 @@ public function database($database, $prefix = MPREFIX, $multiple=false)
return true;
}

if (!@mysql_select_db($database, $this->mySQLaccess))
if (!@mysqli_select_db($this->mySQLaccess, $database))
{
return false;
}
Expand Down Expand Up @@ -361,8 +361,8 @@ function db_Write_log($log_type = '', $log_remark = '', $log_query = '')
*
* 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 object $rli
* @return boolean|resource - as mysql_query() function.
* @param mysqli $rli Your own mysqli connection instead of the one in this object
* @return boolean|mysqli_result - as mysqli_query() function.
* FALSE indicates an error
* For SELECT, SHOW, DESCRIBE, EXPLAIN and others returning a result set, returns a resource
* TRUE indicates success in other cases
Expand Down Expand Up @@ -392,9 +392,9 @@ public function db_Query($query, $rli = NULL, $qry_from = '', $debug = FALSE, $l

$b = microtime();

$sQryRes = is_null($rli) ? @mysql_query($query, $this->mySQLaccess) : @mysql_query($query, $rli);
$this->mySQLlastErrNum = mysql_errno();
$this->mySQLlastErrText = mysql_error();
$sQryRes = is_null($rli) ? @mysqli_query($this->mySQLaccess, $query) : @mysqli_query($rli, $query);
$this->mySQLlastErrNum = mysqli_errno($this->mySQLaccess);
$this->mySQLlastErrText = mysqli_error($this->mySQLaccess);

$e = microtime();

Expand All @@ -416,8 +416,8 @@ public function db_Query($query, $rli = NULL, $qry_from = '', $debug = FALSE, $l
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);
$rc = mysql_fetch_array($fr);
$fr = mysqli_query($this->mySQLaccess, 'SELECT FOUND_ROWS()');
$rc = mysqli_fetch_array($fr);
$this->total_results = (int)$rc['FOUND_ROWS()'];

}
Expand Down Expand Up @@ -587,7 +587,7 @@ public function retrieve($table, $fields = null, $where=null, $multi = false, $i
}

/**
* 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 />
* If you need more requests think to call the class.<br />
* <br />
Expand Down Expand Up @@ -786,7 +786,7 @@ function insert($tableName, $arg, $debug = FALSE, $log_type = '', $log_remark =
{
$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.
{
Expand All @@ -813,10 +813,10 @@ function insert($tableName, $arg, $debug = FALSE, $log_type = '', $log_remark =
{
if(true === $REPLACE)
{
$tmp = mysql_affected_rows($this->mySQLaccess);
$tmp = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Replace');
// $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;
}

Expand All @@ -835,7 +835,7 @@ function insert($tableName, $arg, $debug = FALSE, $log_type = '', $log_remark =

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.
}

Expand All @@ -850,18 +850,18 @@ public function foundRows()
}

/**
* @param resource $result
* @param mysqli_result $result
* @return false|int
*/
public function rowCount($result=null)
{
if (!is_resource($result))
if (!($result instanceof mysqli_result))
{
$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');
return $this->mySQLrows;
Expand Down Expand Up @@ -986,10 +986,10 @@ function update($tableName, $arg, $debug = FALSE, $log_type = '', $log_remark =

if ($result !==false)
{
$result = mysql_affected_rows($this->mySQLaccess);
$result = mysqli_affected_rows($this->mySQLaccess);

$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;
}
else
Expand Down Expand Up @@ -1125,8 +1125,8 @@ function db_UpdateArray($table, $vars, $arg='', $debug = FALSE, $log_type = '',
}
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);
if ($result == -1) return FALSE; // Error return from mysql_affected_rows
$result = mysqli_affected_rows($this->mySQLaccess);
if ($result == -1) return FALSE; // Error return from mysqli_affected_rows
return $result;
}
else
Expand All @@ -1151,7 +1151,7 @@ function truncate($table=null)
/**
* @param string $type assoc|num|both
* @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 :<br />
* <code>while($row = $sql->fetch()){
Expand Down Expand Up @@ -1188,7 +1188,7 @@ function fetch($type = null)

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);
if ($row)
{
Expand Down Expand Up @@ -1233,7 +1233,7 @@ function count($table, $fields = '(*)', $arg = '', $debug = FALSE, $log_type = '
$query=$table;
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');
return (int) $rows['COUNT(*)'];
}
Expand All @@ -1253,7 +1253,7 @@ function count($table, $fields = '(*)', $arg = '', $debug = FALSE, $log_type = '
$query='SELECT COUNT'.$fields.' FROM '.$this->mySQLPrefix.$table.' '.$arg;
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');
return (int) $rows[0];
}
Expand Down Expand Up @@ -1290,8 +1290,8 @@ function close()
{
$this->provide_mySQLaccess();
e107::getSingleton('e107_traffic')->BumpWho('db Close', 1);
$this->mySQLaccess = NULL; // correct way to do it when using shared links.
$this->dbError('dbClose');
$result = mysqli_close($this->mySQLaccess);
if ($result === false) $this->dbError('dbClose');
}


Expand Down Expand Up @@ -1328,7 +1328,7 @@ function delete($table, $arg = '', $debug = FALSE, $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
$this->mySQLrows = mysql_affected_rows($this->mySQLaccess);
$this->mySQLrows = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Delete');
return $this->mySQLrows;
}
Expand All @@ -1342,7 +1342,7 @@ function delete($table, $arg = '', $debug = FALSE, $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');
return $this->mySQLrows;
}
Expand Down Expand Up @@ -1426,8 +1426,8 @@ public function gen($query, $debug = FALSE, $log_type = '', $log_remark = '')
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)
if(preg_match('#^(DELETE|INSERT|REPLACE|UPDATE)#',$query, $matches))
{ // Need to check mysql_affected_rows() - to return number of rows actually updated
$tmp = mysql_affected_rows($this->mySQLaccess);
{ // Need to check mysqli_affected_rows() - to return number of rows actually updated
$tmp = mysqli_affected_rows($this->mySQLaccess);
$this->dbError('db_Select_gen');
return $tmp;
}
Expand Down Expand Up @@ -1462,50 +1462,6 @@ function ml_check($matches)
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.
* @param string|array $table Name of table, without the prefix. or an array of table names.
Expand Down Expand Up @@ -1899,7 +1855,7 @@ function db_Field($table,$fieldid="",$key="", $retinfo = FALSE)

function columnCount()
{
return mysql_num_fields($this->mySQLresult);
return mysqli_num_fields($this->mySQLresult);
}

/**
Expand Down Expand Up @@ -2020,7 +1976,7 @@ function index($table, $keyname, $fields=null, $retinfo = FALSE)


/**
* 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
* @return string
Expand All @@ -2034,7 +1990,7 @@ function escape($data, $strip = true)

$this->provide_mySQLaccess();

return mysql_real_escape_string($data,$this->mySQLaccess);
return mysqli_real_escape_string($this->mySQLaccess, $data);
}


Expand Down Expand Up @@ -2367,13 +2323,13 @@ function backup($table='*', $file='', $options=null)
*/
function dbError($from)
{
$this->mySQLlastErrNum = mysql_errno();
$this->mySQLlastErrNum = mysqli_errno($this->mySQLaccess);
$this->mySQLlastErrText = '';
if ($this->mySQLlastErrNum == 0)
{
return '';
}
$this->mySQLlastErrText = mysql_error(); // Get the error text.
$this->mySQLlastErrText = mysqli_error($this->mySQLaccess); // Get the error text.
if ($this->mySQLerror == TRUE)
{
message_handler('ADMIN_MESSAGE', '<b>mySQL Error!</b> Function: '.$from.'. ['.$this->mySQLlastErrNum.' - '.$this->mySQLlastErrText.']', __LINE__, __FILE__);
Expand Down Expand Up @@ -2439,7 +2395,7 @@ function db_Set_Charset($charset = '', $debug = FALSE)
{
if ( ! $debug)
{
@mysql_query("SET NAMES `$charset`");
@mysqli_query($this->mySQLaccess, "SET NAMES `$charset`");
}
else
{
Expand Down

0 comments on commit 8c528de

Please sign in to comment.