game/_incl_data/mysql_override.php

155 lines
3.7 KiB
PHP

<?php
/**
* Make mysql_connect work like mysqli_connect for future php versions.
*
* @author Ivor Barhansky
* @version 0.2
*/
// Make sure the MySQL extension is not loaded and there is no other drop in replacement active
if (!extension_loaded('mysql') && !function_exists('mysql_connect')) {
// Validate if the MySQLi extension is present
if (!extension_loaded('mysqli')) {
trigger_error('The extension "MySQLi" is not available', E_USER_ERROR);
}
// The function name "getLinkIdentifier" will be used to return a valid link_indentifier, make it is available
if (function_exists('getLinkIdentifier')) {
trigger_error('The function name "getLinkIdentifier" is already defined, please change the function name', E_USER_ERROR);
}
// Define MySQL constants
define('MYSQL_CLIENT_COMPRESS', MYSQLI_CLIENT_COMPRESS);
define('MYSQL_CLIENT_IGNORE_SPACE', MYSQLI_CLIENT_IGNORE_SPACE);
define('MYSQL_CLIENT_INTERACTIVE', MYSQLI_CLIENT_INTERACTIVE);
define('MYSQL_CLIENT_SSL', MYSQLI_CLIENT_SSL);
define('MYSQL_ASSOC', MYSQLI_ASSOC);
define('MYSQL_NUM', MYSQLI_NUM);
define('MYSQL_BOTH', MYSQLI_BOTH);
// Will contain the link identifier
$link = null;
/**
* Get the link identifier
*
* @param mysqli $mysqli
* @return mysqli|null
*/
function getLinkIdentifier(mysqli $mysqli = null)
{
if (!$mysqli) {
global $link;
$mysqli = $link;
}
return $mysqli;
}
/**
* Open a connection to a MySQL Server
*
* @param $server
* @param $username
* @param $password
* @return mysqli|null
* @deprecated
*/
function mysql_connect($server, $username, $password, $new_link = false, $client_flags = 0)
{
global $link;
$link = mysqli_connect($server, $username, $password);
return $link;
}
/**
* @param $databaseName
* @return bool
* @deprecated
*/
function mysql_select_db($databaseName)
{
global $link;
return mysqli_select_db($link, $databaseName);
}
/**
* @param $query
* @param mysqli $mysqli
* @return bool|mysqli_result
* @deprecated
*/
function mysql_query($query, mysqli $mysqli = null)
{
return getLinkIdentifier($mysqli)->query($query);
}
/**
* @param $string
* @param mysqli $mysqli
* @return string
* @deprecated
*/
function mysql_real_escape_string($string, mysqli $mysqli = null)
{
return getLinkIdentifier($mysqli)->escape_string($string);
}
/**
* @param mysqli_result $result
* @return bool|array
* @deprecated
*/
function mysql_fetch_assoc(mysqli_result $result)
{
$result = $result->fetch_assoc();
if ($result === null) {
$result = false;
}
return $result;
}
/**
* @param mysqli_result $result
* @return bool|int
* @deprecated
*/
function mysql_num_rows(mysqli_result $result)
{
$result = $result->num_rows;
if ($result === null) {
$result = false;
}
return $result;
}
/**
* @param mysqli_result $result
* @param $result_type
* @return void
* @deprecated
*/
function mysql_fetch_array(mysqli_result $result, $result_type = MYSQL_BOTH)
{
return mysqli_fetch_array($result, $result_type);
}
/**
* Get the ID generated in the last query
*
* @param null $mysqli
* @return int|string
* @deprecated
*/
function mysql_insert_id(mysqli $mysqli = null)
{
return mysqli_insert_id(getLinkIdentifier($mysqli));
}
}