-
-
Save blizzrdof77/5c99ce99a79b6b15562cdbc43ac5520e to your computer and use it in GitHub Desktop.
Legacy PHP & MySQL Compatibility Functions - Replaces all MySQL functions with the corresponding MySQLi functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Legacy PHP & MySQL Compatibility Functions | |
*/ | |
/** | |
* replacement for all mysql functions | |
* | |
* Be aware, that this is just a workaround to fix-up some old code and the resulting project | |
* will be more vulnerable than if you use the recommended newer mysqli-functions instead. | |
* So only If you are sure that this is not setting your server at risk, you can fix your old | |
* code by adding this line at the beginning of your old code: | |
* | |
* see: https://stackoverflow.com/a/37877644/1069083 | |
*/ | |
if (!function_exists("mysql_connect")): | |
/* | |
* WARNING: | |
* | |
* fatal error "cannot redeclare" if a function was disabled in php.ini with disable_functions: | |
* | |
* disable_functions=mysql_connect,mysql_pconnect,mysql_select_db,mysql_ping,mysql_query, | |
* mysql_fetch_assoc,mysql_num_rows,mysql_fetch_array,mysql_error,mysql_insert_id,mysql_close, | |
* mysql_real_escape_string,mysql_data_seek,mysql_result | |
*/ | |
function mysql_connect($host, $username, $password) { | |
global $dbconnect; | |
$dbconnect = mysqli_connect($host, $username, $password); | |
return $dbconnect; | |
} | |
endif; | |
if (!function_exists("mysql_pconnect")): | |
function mysql_pconnect($host, $username, $password) { | |
global $dbconnect; | |
$dbconnect = mysqli_connect("p:" . $host, $username, $password); | |
return $dbconnect; | |
} | |
endif; | |
if (!function_exists('mysql_select_db')): | |
function mysql_select_db($db, $dbconnect) { | |
return mysqli_select_db($dbconnect, $db); | |
} | |
endif; | |
if (!function_exists('mysql_ping')): | |
function mysql_ping($dbconnect) { | |
return mysqli_ping($dbconnect); | |
} | |
endif; | |
if (!function_exists('mysql_query')): | |
function mysql_query($stmt) { | |
global $dbconnect; | |
return mysqli_query($dbconnect, $stmt); | |
} | |
endif; | |
if (!function_exists('mysql_fetch_assoc')): | |
function mysql_fetch_assoc($erg) { | |
return mysqli_fetch_assoc($erg); | |
} | |
endif; | |
if (!function_exists('mysql_num_rows')): | |
function mysql_num_rows($e) { | |
return mysqli_num_rows($e); | |
} | |
endif; | |
if (!function_exists('mysql_affected_rows')): | |
function mysql_affected_rows($e = null) { | |
// TODO: still not working when called without argument: mysql_affected_rows() | |
return mysqli_affected_rows($e); | |
} | |
endif; | |
if (!function_exists('mysql_fetch_array')): | |
function mysql_fetch_array($e) { | |
return mysqli_fetch_array($e); | |
} | |
endif; | |
if (!function_exists('mysql_error')): | |
function mysql_error() { | |
global $dbconnect; | |
return mysqli_error($dbconnect); | |
} | |
endif; | |
if (!function_exists('mysql_insert_id')): | |
function mysql_insert_id($cnx) { | |
return mysqli_insert_id($cnx); | |
} | |
endif; | |
if (!function_exists('mysql_close')): | |
function mysql_close() { | |
return true; | |
} | |
endif; | |
if (!function_exists('mysql_real_escape_string')): | |
function mysql_real_escape_string($s) { | |
global $dbconnect; | |
return mysqli_real_escape_string($dbconnect, $s); | |
} | |
endif; | |
if (!function_exists('mysql_data_seek')): | |
function mysql_data_seek($re, $row) { | |
return mysqli_data_seek($re, $row); | |
} | |
endif; | |
if (!function_exists('mysql_result')): | |
function mysql_result($res, $row = 0, $col = 0) { | |
$numrows = mysqli_num_rows($res); | |
if ($numrows && $row <= ($numrows - 1) && $row >= 0) { | |
mysqli_data_seek($res, $row); | |
$resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res); | |
if (isset($resrow[$col])) { | |
return $resrow[$col]; | |
} | |
} | |
return false; | |
} | |
endif; | |
if (!function_exists('mysql_escape_string')): | |
function mysql_escape_string($s) { | |
global $dbconnect; | |
return mysqli_real_escape_string($dbconnect, $s); | |
} | |
endif; | |
if (!function_exists('mysql_fetch_row')): | |
function mysql_fetch_row($e) { | |
return mysqli_fetch_row($e); | |
} | |
endif; | |
if (!function_exists('mysql_errno')): | |
function mysql_errno($db) { | |
return mysqli_errno($db); | |
} | |
endif; | |
if (!function_exists('mysql_fetch_object')): | |
function mysql_fetch_object($erg) { | |
return mysqli_fetch_object($erg); | |
} | |
endif; | |
if (!function_exists('mysql_field_name')): | |
function mysql_field_name($result, $i) { | |
return mysqli_fetch_field_direct($result, $i); | |
} | |
endif; | |
if (!function_exists('mysql_free_result')): | |
function mysql_free_result($result) { | |
return mysqli_free_result($result); | |
} | |
endif; | |
if (!function_exists('mysql_get_server_info')): | |
function mysql_get_server_info() { | |
global $dbconnect; | |
return mysqli_get_server_info($dbconnect); | |
} | |
endif; | |
if (!function_exists('mysql_set_charset')): | |
function mysql_set_charset($csname) { | |
global $dbconnect; | |
return mysqli_set_charset($dbconnect, $csname); | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment