🧩 PHP 7 이상에서 mysql_connect() 오류 해결법 — mysql-wrapper.php로 완벽 호환하기
PHP 7 이후로 기존의 mysql_* 계열 함수들이 완전히 제거되면서,
기존에 mysql_connect(), mysql_query(), mysql_fetch_array() 등을 사용하던
레거시 코드들은 모두 Fatal error를 발생시키게 되었습니다.
❌ Fatal error: Uncaught Error: Call to undefined function mysql_connect()
이런 상황에서 “예전 코드를 다 mysqli_*로 바꾸는 건 너무 많은 작업이다…”
라고 고민하신 적 있으신가요?
그럴 때 유용한 해결책이 바로 mysql-wrapper.php 입니다.
이 파일 하나만 추가하면, 구형 MySQL 함수들이 그대로 동작하도록 만들어줍니다.
🧠 mysql-wrapper.php란?
mysql-wrapper.php는 PHP 7 이상 버전에서 제거된 mysql_* 함수들을mysqli_* 함수로 자동 매핑(에뮬레이션) 해주는 호환성 스크립트입니다.
즉, 예전 PHP 5.x 코드처럼 이렇게 써도:
$conn = mysql_connect("localhost", "root", "1234");
mysql_select_db("testdb", $conn);
$result = mysql_query("SELECT * FROM users");
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']."<br>";
}
PHP 7~8 환경에서도 정상 작동하게 됩니다.
내부적으로는 mysqli_connect(), mysqli_query() 등이 호출되어 처리됩니다.
⚙️ 동작 원리
이 스크립트는 PHP의 mysqli 확장을 기반으로 하며,
각 구형 함수들을 새로운 함수로 에뮬레이트(모방) 합니다.
| 구형 함수 | 내부 대체 함수 |
|---|---|
mysql_connect() | mysqli_connect() |
mysql_query() | mysqli_query() |
mysql_fetch_array() | mysqli_fetch_array() |
mysql_fetch_assoc() | mysqli_fetch_assoc() |
mysql_num_rows() | mysqli_num_rows() |
mysql_error() | mysqli_error() |
mysql_insert_id() | mysqli_insert_id() |
이외에도 mysql_free_result(), mysql_real_escape_string() 등
대부분의 함수가 완벽히 구현되어 있습니다.
🧰 사용 방법
- 아래의
mysql-wrapper.php파일을 다운로드하거나 코드로 생성합니다. - 프로젝트 내에 업로드합니다. (예:
/lib/mysql-wrapper.php) - 기존 PHP 코드 상단에 다음 한 줄만 추가하세요:
require_once 'mysql-wrapper.php'; - 나머지 코드는 수정 없이 그대로 사용 가능합니다.
🚀 예제 코드
<?php
require_once 'mysql-wrapper.php';
$conn = mysql_connect("localhost", "root", "1234");
mysql_select_db("testdb", $conn);
$result = mysql_query("SELECT * FROM users WHERE level = 1");
while ($row = mysql_fetch_assoc($result)) {
echo $row['username']." / ".$row['email']."<br>";
}
?>
이렇게 작성된 예전 PHP 5 코드도 PHP 8.3 환경에서 문제없이 실행됩니다.
⚠️ 주의할 점
- 이 스크립트는 호환성 목적입니다.
새 프로젝트에서는mysqli나PDO를 직접 사용하는 것이 안전합니다. mysql_real_escape_string()같은 구문은 여전히 수동 처리 방식이므로,
SQL Injection 방어가 완벽하지 않습니다.- 오래된 구조의 CMS(예: 구형 XE, Allplan, 자체 쇼핑몰 솔루션 등) 유지용으로 적합합니다.
💡 팁: 장기적으로는 mysqli로 이전하세요
이 wrapper는 “임시 브릿지” 역할입니다.
프로젝트를 안정화시킨 후 아래처럼 직접 mysqli 문법으로 마이그레이션하는 것을 추천드립니다.
| 기존 코드 | 개선 코드 |
|---|---|
mysql_connect() | $conn = mysqli_connect("localhost", "user", "pass", "db"); |
mysql_query() | mysqli_query($conn, "SELECT * FROM table"); |
mysql_fetch_assoc() | mysqli_fetch_assoc($result); |
✅ 결론
mysql-wrapper.php는 PHP 7 이상에서
*기존 mysql_ 기반 소스들을 그대로 살릴 수 있는 구조적 해법**입니다.
오래된 PHP 쇼핑몰, 사내 시스템, 레거시 ERP를 운영 중이라면
이 파일 하나로 마이그레이션 부담을 대폭 줄일 수 있습니다.
🔗 추천 사용 예: 구형 PHP 5 기반 솔루션을 CentOS 8 / Ubuntu 22 환경으로 이전할 때
🧾 요약
- ✅ PHP 7~8에서도
mysql_*함수 사용 가능 - ⚙️ 내부적으로
mysqli확장으로 완벽 매핑 - ⚠️ 보안 기능은 취약하므로 점진적 전환 필요
- 🧰 기존 코드 수정 없이 바로 호환
mysql-wrapper.php
<?php
function is_myresource ($o, $onlyres = false) {
if ( extension_loaded ('mysql') ) {
if ( ! is_resource ($o) )
return false;
$cname = get_resource_type ($o);
if ( $cname != 'mysql link' && $cname != 'mysql result' )
return false;
if ( $onlyres && $cname != 'mysql result' )
return false;
unset ($cname);
return true;
}
if ( ! is_object ($o) )
return false;
$cname = get_class ($o);
if ( $cname != 'mysqli' && $cname != 'mysqli_result' )
return false;
if ( $onlyres && $cname != 'mysqli_result' )
return false;
unset ($cname);
return true;
}
if ( ! function_exists ('mysql_connect') ) {
if ( ! extension_loaded ('mysqli') ) {
throw new Exception (E_ERROR, 'MySQL wrapper must need mysqli extension');
}
if ( ! function_exists ('___ini_get') ) {
function ___ini_get ($v) { return ini_get ($v); }
}
$_MySQLCON_ = null;
$_MyConnErr_ = null;
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_BOTH', MYSQLI_BOTH);
define ('MYSQL_NUM', MYSQLI_NUM);
if ( ! defined ('MYSQLI_NO_DEFAULT_VALUE_FLAG') )
define ('MYSQLI_NO_DEFAULT_VALUE_FLAG', -1);
if ( ! defined ('MYSQLI_ON_UPDATE_NOW_FLAG') )
define ('MYSQLI_ON_UPDATE_NOW_FLAG', -1);
$msyql_filed_flags = array (
MYSQLI_NOT_NULL_FLAG,
MYSQLI_PRI_KEY_FLAG,
MYSQLI_UNIQUE_KEY_FLAG,
MYSQLI_MULTIPLE_KEY_FLAG,
MYSQLI_BLOB_FLAG,
MYSQLI_UNSIGNED_FLAG,
MYSQLI_ZEROFILL_FLAG,
MYSQLI_AUTO_INCREMENT_FLAG,
MYSQLI_TIMESTAMP_FLAG,
MYSQLI_SET_FLAG,
MYSQLI_NUM_FLAG,
MYSQLI_PART_KEY_FLAG,
MYSQLI_GROUP_FLAG,
MYSQLI_ENUM_FLAG,
MYSQLI_BINARY_FLAG,
MYSQLI_NO_DEFAULT_VALUE_FLAG,
MYSQLI_ON_UPDATE_NOW_FLAG
);
$mysql_data_type_hash = array (
0 => 'real', // decimal
1 => 'int', // tiny int
2 => 'int', // smallint
3 => 'int', // int
4 => 'real', // float
5 => 'real', // double
6 => 'null', // null
7 => 'timestamp', // timestamp
8 => 'int', // bigint
9 => 'int', // mediumint
10 => 'date', // date
11 => 'time', // time
12 => 'datetime', // datetime
13 => 'year', // year
14 => 'date', // newdate
15 => 'string', // varchar
16 => 'int', // bit
246 => 'real', // newdecimal
247 => 'string', // enum
248 => 'string', // set
249 => 'blob', // tibyblob
250 => 'blob', // mediumblob
251 => 'blob', // longblob
252 => 'blob', // blob
253 => 'string', // string
254 => 'string', // string
246 => 'real' // decimal
);
function mysql_affected_rows (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_affected_rows ($c);
}
function mysql_client_encoding (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_character_set_name ($c);
}
function mysql_close (&$c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_close ($c);
}
function mysql_connect ($host = null, $user = null, $pass = null, $link = false, $flag = 0) {
$GLOBALS['_MyConnErr_'] = null;
if ( $host === null )
$host = ___ini_get ('mysqli.default_host');
if ( $user === null )
$user = ___ini_get ('mysqli.default_user');
if ( $pass === null )
$pass = ___ini_get ('mysqli.default_pw');
$persistant = false;
$socket = null;
$port = ___ini_get ('mysqli.default_port');
if ( $host[0] === ':' ) {
$socket = substr ($host, 1);
$host = 'localhost';
} else {
if ( preg_match ('/^p:/', $host) ) {
$persistant = true;
$host = substr ($host, 2);
}
if ( preg_match ('/^([^:]+):([\d]+)$/', $host, $m) ) {
$host = $m[1];
$port = $m[2];
}
}
if ( $persistant === true )
$host = 'p:' . $host;
$c = mysqli_connect ($host, $user, $pass, '', $port, $socket);
$GLOBALS['_MyConnErr_'] = error_get_last ();
#if ( $GLOBALS['_MySQLCON_'] === null && is_myresource ($c) )
if ( is_myresource ($c) )
$GLOBALS['_MySQLCON_'] = &$c;
return $c;
}
function mysql_create_db ($name, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
$name = trim ($name);
if ( ! $name )
return false;
return mysqli_query ($c, 'CREATE DATABASE ' . $name);
}
function mysql_createdb ($name, $c = null) {
return mysql_create_db ($name, $c);
}
function mysql_data_seek ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_data_seek ($result, $offset);
}
function mysql_db_name ($result, $row, $field = 'Database') {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $row < 0 || $row >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $row, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
mysqli_data_seek ($result, $row);
if ( ! ($res = mysqli_fetch_object ($result)) )
return false;
return $res->{$field};
}
function mysql_db_query ($db, $query, $c = null) {
if ( ($c = mysql_global_resource ($c, 3 - func_num_args ())) == null )
return;
$curdb = mysql_get_current_database ($c);
if ( mysqli_select_db ($c, $db) === false )
return false;
$r = mysqli_query ($c, $query);
if ($curdb !== null)
mysqli_select_db($c, $curdb);
return $r;
}
function mysql_drop_db ($db, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
$db = trim ($db);
if ( ! $db )
return false;
return mysqli_query ($c, sprintf ('DROP DATABASE %s', $db));
}
function mysql_errno ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args (), true)) == null )
return -1;
return mysqli_errno ($c);
}
function mysql_error ($c = null) {
$c = mysql_global_resource ($c, 1 - func_num_args (), true);
if ( ! is_myresource ($c) ) {
if ( is_array ($GLOBALS['_MyConnErr_']) && trim ($GLOBALS['_MyConnErr_']['message']) ) {
preg_match ('/[a-z_]+\(\):\s+\([^)]+\):\s+(.+)/i', $GLOBALS['_MyConnErr_']['message'], $msg);
return $msg[1];
}
return null;
}
return mysqli_error ($c);
}
if ( ! function_exists ('mysql_escape_string') ) {
function mysql_escape_string ($escape) {
return mysqli_real_escape_string ($GLOBALS['_MySQLCON_'], $escape);
}
}
function mysql_fetch_array ($result, $type = MYSQLI_BOTH) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_array ($result, $type);
if ( $r === null )
$r = false;
return $r;
}
function mysql_fetch_assoc ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysql_fetch_array ($result, MYSQLI_ASSOC);
}
function mysql_fetch_field ($result, $offset = null) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset !== null ) {
if ( $offset < 0 || $offset >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$res = mysqli_fetch_field_direct ($result, $offset);
} else
$res = mysqli_fetch_field ($result);
if ( $res === false )
return false;
$r = (object) array (
'name' => $res->name,
'table' => $res->table,
'def' => '', // default value
'max_length' => $res->max_length,
'not_null' => ($res->flags & MYSQLI_NOT_NULL_FLAG) ? 1 : 0,
'primary_key' => ($res->flags & MYSQLI_PRI_KEY_FLAG) ? 1 : 0,
'unique_key' => ($res->flags & MYSQLI_UNIQUE_KEY_FLAG) ? 1 : 0,
'multiple_key' => ($res->flags & MYSQLI_MULTIPLE_KEY_FLAG) ? 1 : 0,
'numeric' => ($res->flags & MYSQLI_NUM_FLAG) ? 1 : 0,
'blob' => ($res->flags & MYSQLI_BLOB_FLAG) ? 1 : 0,
'type' => $GLOBALS['mysql_data_type_hash'][$res->type],
'unsigned' => ($res->flags & MYSQLI_UNSIGNED_FLAG) ? 1 : 0,
'zerofill' => ($res->flags & MYSQLI_ZEROFILL_FLAG) ? 1 : 0
);
// exception
if ( $res->flags === 0 ) // decimal type
$r->numeric = 1;
if ( $r->type == 'timestamp' )
$r->numeric = 1;
return $r;
}
function mysql_fetch_lengths ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_fetch_lengths ($result);
}
function mysql_fetch_object ($result, $classname = null, $params = null) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $classname !== null ) {
if ( ! class_exists ($classname) ) {
$msg = sprintf ('Class \'%s\' not found', $classname);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $params !== null ) {
if ( ! is_array ($params) ) {
$msg = 'Argument 3 passed to mysql_fetch_object() must be of the type array';
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_object ($result, $classname, $params);
} else
$r = mysqli_fetch_object ($result, $classname);
} else
$r = mysqli_fetch_object ($result);
return ($r === null) ? false : $r;
}
function mysql_fetch_row ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$r = mysqli_fetch_row ($result);
return ($r === null ) ? false : $r;
}
function mysql_field_flags ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ($r = mysqli_fetch_field_direct ($result, $offset)) === false ) {
return false;
}
$res = false;
foreach ( $GLOBALS['msyql_filed_flags'] as $flag ) {
#printf ("#### {$r->flags} : {$flag} => %d\n", ($r->flags & $flag));
if ( ! ($r->flags & $flag) )
continue;
switch ( $flag ) {
case MYSQLI_NOT_NULL_FLAG :
$res .= 'not_null '; break;
case MYSQLI_PRI_KEY_FLAG :
$res .= 'primary_key '; break;
case MYSQLI_UNIQUE_KEY_FLAG :
$res .= 'unique_key '; break;
case MYSQLI_MULTIPLE_KEY_FLAG :
$res .= 'multiple_key '; break;
case MYSQLI_BLOB_FLAG :
$res .= 'blob '; break;
case MYSQLI_UNSIGNED_FLAG :
$res .= 'unsigned '; break;
case MYSQLI_ZEROFILL_FLAG :
$res .= 'zerofill '; break;
case MYSQLI_AUTO_INCREMENT_FLAG :
$res .= 'auto_increment '; break;
case MYSQLI_TIMESTAMP_FLAG :
$res .= 'timestamp '; break;
case MYSQLI_SET_FLAG :
$res .= 'set '; break;
//case MYSQLI_NUM_FLAG :
// $res .= 'numeric '; break;
case MYSQLI_PART_KEY_FLAG :
$res .= 'part_key '; break;
//case MYSQLI_GROUP_FLAG :
// $res .= 'group '; break;
case MYSQLI_ENUM_FLAG :
$res .= 'enum '; break;
case MYSQLI_BINARY_FLAG :
$res .= 'binary '; break;
//case MYSQLI_NO_DEFAULT_VALUE_FLAG :
// $res .= 'no_default_value '; break;
case MYSQLI_ON_UPDATE_NOW_FLAG :
$res .= 'on_update_now '; break;
}
}
return rtrim ($res);
}
function mysql_field_len ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r->length;
}
function mysql_field_name ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r->name;
}
function mysql_field_seek ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ! isset ($offset) ) {
$msg = sprintf ('expects parameter 2 to be offset value');
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= mysqli_num_fields ($result) ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_field_seek ($result, $offset);
}
function mysql_field_table ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r->table;
}
function mysql_field_type ($result, $offset) {
if ( ($r = mysqli_mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $GLOBALS['mysql_data_type_hash'][$r->type];
}
function mysql_free_result ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
mysqli_free_result ($result);
return true;
}
function mysql_get_client_info () {
return mysqli_get_client_info ($GLOBALS['_MySQLCON_']);
}
function mysql_get_host_info ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_get_host_info ($c);
}
function mysql_get_proto_info ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_get_proto_info ($c);
}
function mysql_get_server_info ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_get_server_info ($c);
}
function mysql_info ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_info ($c);
}
function mysql_insert_id ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_insert_id ($c);
}
function mysql_list_dbs ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_query ($c, 'SHOW DATABASES');
}
function mysql_list_fields ($db, $table, $c = null) {
if ( ($c = mysql_global_resource ($c, 3 - func_num_args ())) == null )
return;
$r = mysql_db_query ($db, sprintf ('SELECT * FROM %s LIMIT 0', $table), $c);
return $r;
}
function mysql_list_processes ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_query ($c, 'SHOW PROCESSLIST');
}
function mysql_list_tables ($database, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
return mysqli_query ($c, sprintf ('SHOW TABLES FROM %s', $database));
}
function mysql_num_fields ($result) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
return mysqli_num_fields ($result);
}
function mysql_num_rows ($result) {
if ( ! is_myresource ($result, true) ) {
trigger_error (
mysql_trigger_msg(
'supplied resource is not a valid MySQL result resource',
current (debug_backtrace ())
),
E_USER_ERROR
);
return false;
}
return mysqli_num_rows ($result);
}
function mysql_pconnect ($host = null, $user = null, $pass = null, $link = false, $flag = 0) {
return mysql_connect ('p:' . $host, $user, $pass, $link, $flag);
}
function mysql_ping ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_ping ($c);
}
function mysql_query ($query, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
return mysqli_query ($c, $query);
}
if ( ! function_exists ('mysql_real_escape_string') ) {
function mysql_real_escape_string ($escape, $c = null) {
if (($c = mysql_global_resource ($c, 2 - func_num_args())) == null )
return;
return mysqli_real_escape_string($c, $escape);
}
}
function mysql_result ($result, $row, $field = 0) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $row < 0 || $row >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $row, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $field < 0 || $field >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $field, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
$res = &$result;
mysqli_data_seek ($res, $row);
$r = mysqli_fetch_array ($res, MYSQLI_NUM);
return $r[$field];
}
function mysql_select_db ($db, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
return mysqli_select_db ($c, $db);
}
function mysql_set_charset ($charset, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
return mysqli_set_charset ($c, $charset);
}
function mysql_stat ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_stat ($c);
}
function mysql_tablename ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
__FUNCTION__, gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->num_rows ) {
$msg = sprintf ('Unable to jump to row %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, current (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
mysqli_data_seek ($result, $offset);
$r = mysqli_fetch_array ($result, MYSQLI_NUM);
return $r[0];
}
function mysql_thread_id ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
return mysqli_thread_id ($c);
}
function mysql_unbuffered_query ($query, $c = null) {
if ( ($c = mysql_global_resource ($c, 2 - func_num_args ())) == null )
return;
return mysqli_query ($c, $query, MYSQLI_USE_RESULT);
}
function mysql_get_current_database ($c = null) {
if ( ($c = mysql_global_resource ($c, 1 - func_num_args ())) == null )
return;
$r = mysqli_query ($c, 'SELECT DATABASES() AS curdb');
if ( ! is_object ($r) )
return null;
$row = mysqli_fetch_object ($r);
return ($row->curdb == 'NULL') ? null : $row->curdb;
}
function mysqli_mysqli_fetch_field_direct ($result, $offset) {
if ( ! is_myresource ($result, true) ) {
$msg = sprintf (
'expects parameter 1 to be mysql result object, %s given',
gettype ($result)
);
trigger_error (
mysql_trigger_msg ($msg, next (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ! isset ($offset) ) {
$msg = sprintf ('expects parameter 2 to be offset value');
trigger_error (
mysql_trigger_msg ($msg, next (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( $offset < 0 || $offset >= $result->field_count ) {
$msg = sprintf ('Unable to jump to field %ld on MySQL result index %d', $offset, $result);
trigger_error (
mysql_trigger_msg ($msg, next (debug_backtrace ())),
E_USER_WARNING
);
return false;
}
if ( ($r = mysqli_fetch_field_direct ($result, $offset)) === false )
return false;
return $r;
}
function mysql_global_resource (&$c, $argc, $noerr = false) {
if ( $argc < 0 ) {
trigger_error (
mysql_trigger_msg('Wrong argument numers', next (debug_backtrace ())),
E_USER_WARNING
);
return null;
}
// $c is exists
if ( $argc == 0 ) {
if ( ! is_myresource ($c) ) {
if ( $noerr === false ) {
trigger_error (
mysql_trigger_msg ('no MySQL-Link resource supplied', next (debug_backtrace ())),
E_USER_WARNING
);
}
return null;
}
} else {
$c = &$GLOBALS['_MySQLCON_'];
if ( ! is_myresource ($c) ) {
if ( $noerr === false ) {
trigger_error (
mysql_trigger_msg ('no MySQL-Link resource supplied', next (debug_backtrace ())),
E_USER_WARNING
);
}
return null;
}
}
return $c;
}
function mysql_trigger_msg ($msg, $tr) {
return sprintf ('%s: %s in %s on lien %d', $tr['function'], $msg, $tr['file'], $tr['line']);
}
}
