php Mysql 和Mysqli数据库函数整合

时间:2022-04-20 21:40:07

PHP Mysql和Mysqli数据库函数整合

服务器如果支持mysqli函数将优先mysqli函数进行数据库操作 否则将调用mysql函数进行数据库操作

用法SQL::connect(host,user,pass).....

本类所实现的方法不可传递数据库连接对象。程序已自动传递

<?php
/**
* User: iyoule
* Date: 13-9-11
* Time: 下午11:48
* 用法:SQL::connect(host,user,pass).....
* To change this template use File | Settings | File Templates.
*/ class SQL {
private static $sqlArr = array(
'fetch_array'=> array('mysql_fetch_array','mysqli_fetch_array'),
'fetch_assoc'=> array('mysql_fetch_assoc','mysqli_fetch_assoc'),
'fetch_field'=> array('mysql_fetch_field','mysqli_fetch_field'),
'fetch_lengths'=> array('mysql_fetch_lengths','mysqli_fetch_lengths'),
'fetch_object'=> array('mysql_fetch_object','mysqli_fetch_object'),
'fetch_row'=> array('mysql_fetch_row','mysqli_fetch_row'),
'field_seek'=> array('mysql_field_seek','mysqli_field_seek'),
'free_result'=> array('mysql_free_result','mysqli_free_result'),
'connect'=> array('mysql_connect','mysqli_connect'),
'data_seek'=> array('mysql_data_seek','mysqli_data_seek'),
'affected_rows' => array('mysql_affected_rows','mysqli_affected_rows'),
'client_encoding'=> array('mysql_client_encoding','mysqli_client_encoding'),
'close'=> array('mysql_close','mysqli_close'),
'errno'=> array('mysql_errno','mysqli_errno'),
'error'=> array('mysql_error','mysqli_error'),
'escape_string'=> array('mysql_escape_string','mysqli_escape_string'),
'get_client_info'=> array('mysql_get_client_info','mysqli_get_client_info'),
'get_host_info'=> array('mysql_get_host_info','mysqli_get_host_info'),
'get_proto_info'=> array('mysql_get_proto_info','mysqli_get_proto_info'),
'get_server_info'=> array('mysql_get_server_info','mysqli_get_server_info'),
'info'=> array('mysql_info','mysqli_info'),
'insert_id'=> array('mysql_insert_id','mysqli_insert_id'),
'ping'=> array('mysql_ping','mysqli_ping'),
'query'=> array('mysql_query','mysqli_query'),
'real_escape_string'=> array('mysql_real_escape_string','mysqli_real_escape_string'),
'select_db'=> array('mysql_select_db','mysqli_select_db'),
'set_charset'=> array('mysql_set_charset','mysqli_set_charset'),
'stat'=> array('mysql_stat','mysqli_stat'),
'thread_id'=> array('mysql_thread_id','mysqli_thread_id'),
'num_fields'=> array('mysql_num_fields','mysqli_num_fields'),
'num_rows'=> array('mysql_num_rows','mysqli_num_rows') );
private static $resource = null;
private static $funcs = array(
'affected_rows','client_encoding','close','errno','error','escape_string','get_client_info','get_host_info','get_proto_info',
'get_proto_info','get_server_info','info','insert_id','ping','query','real_escape_string','select_db','set_charset','stat',
'thread_id'
);
private static function _globalFunc_($func=__function__,$args){
if($func==='_globalFunc_') return false;
$_Mysqli = extension_loaded('mysqli');
$sqlArr = self::$sqlArr;
$_func = $func;
$func = $_Mysqli?$sqlArr[$func][1]:$sqlArr[$func][0];
if($_func=='connect'){
return self::$resource = self::$resource===null?call_user_func_array($func,$args):self::$resource;
}
if(in_array($_func,self::$funcs)){
$_Mysqli?array_unshift($args,self::$resource):array_push($args,self::$resource);
}
unset($sqlArr,$_Mysqli);
return call_user_func_array($func,$args);
}
public static function affected_rows(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function client_encoding(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function close(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function connect(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function data_seek(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function errno(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function error(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function escape_string(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_array(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_assoc(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_field(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_lengths(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_object(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function fetch_row(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function field_seek(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function free_result(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function get_client_info(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function get_host_info(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function get_proto_info(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function get_server_info(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function info(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function insert_id(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function num_fields(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function num_rows(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function ping(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function query(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function real_escape_string(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function select_db(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function set_charset(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function stat(){
return self::_globalFunc_(__function__,func_get_args());
}
public static function thread_id(){
return self::_globalFunc_(__function__,func_get_args());
}
private function __destruct(){
self::close();
self::$resource=null;
}
}