<?php
class class_mysql
{
public $db_master=array("0"=>array('127.0.0.1','root',''));
public $db_slave=array("0"=>array('127.0.0.1','root',''));
public $db_database='test'; //数据库名
public $sql,$tmpConn,$UseMainDB=false;
private $conn_master,$conn_slave,$queryID,$dbhash,$starttime=0,$timeline=0,$timezone=0,$error_message= array();
//-----------------------------------------------------------------
function __construct()
{
$this->starttime = time();
}
//-----------------------------------------------------------------
function connect_master()
{
if(!is_dir('data/log')){@mkdir('data/log/', 0777);}
if(!is_dir('data/log/sql_errs/')){@mkdir('data/log/sql_errs/', 0777);}
if(!is_dir('data/log/sql_logs/')){@mkdir('data/log/sql_logs/', 0777);}
if(extension_loaded('mysqli'))
{
if(count($this->db_master)>1)
{
$arr=$this->db_master[array_rand($this->db_master , 1)];
$this->conn_master =new mysqli($arr[0],$arr[1],$arr[2],$this->db_database) ;
if ($this->conn_master->connect_errno)
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='master_'.$this->db_database;
}
else
{
$arr=$this->db_master[0] ;
$this->conn_master =new mysqli($arr[0],$arr[1],$arr[2],$this->db_database) ;
if ($this->conn_master->connect_errno)
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='master_'.$this->db_database;
}
$this->conn_master->query("SET NAMES 'UTF8'");
$this->conn_master->query("SET sql_mode=''");
}
else
{
if(count($this->db_master)>1)
{
$arr=$this->db_master[array_rand($this->db_master , 1)];
if(!($this->conn_master =mysql_connect($arr[0],$arr[1],$arr[2])))
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='master_'.$this->db_database;
}
else
{
$arr=$this->db_master[0] ;
if(!($this->conn_master =mysql_connect($arr[0],$arr[1],$arr[2])))
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='master_'.$this->db_database;
}
if(!(mysql_select_db($this->db_database, $this->conn_master)))
{
$this->ErrorMsg("Can't Select MySQL Server Database(".$arr[0].")!");
return false;
}
mysql_query("SET NAMES 'UTF8'");
}
}
//-----------------------------------------------------------------
function connect_slave()
{
if(!is_dir('data/log')){@mkdir('data/log/', 0777);}
if(!is_dir('data/log/sql_errs/')){@mkdir('data/log/sql_errs/', 0777);}
if(!is_dir('data/log/sql_logs/')){@mkdir('data/log/sql_logs/', 0777);}
if(extension_loaded('mysqli'))
{
if(count($this->db_slave)>1)
{
$arr=$this->db_slave[array_rand($this->db_slave , 1)];
$this->conn_slave =new mysqli($arr[0],$arr[1],$arr[2],$this->db_database) ;
if ($this->conn_slave->connect_errno) {
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='slave_'.$this->db_database;
}
else
{
$arr=$this->db_slave[0] ;
$this->conn_slave =new mysqli($arr[0],$arr[1],$arr[2],$this->db_database) ;
if ($this->conn_slave->connect_errno) {
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='slave_'.$this->db_database;
}
$this->conn_slave->query("SET NAMES 'UTF8'");
$this->conn_slave->query("SET sql_mode=''");
}
else
{
if(count($this->db_slave)>1)
{
$arr=$this->db_slave[array_rand($this->db_slave , 1)];
if(!($this->conn_slave =mysql_connect($arr[0],$arr[1],$arr[2])))
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='slave_'.$this->db_database;
}
else
{
$arr=$this->db_slave[0] ;
if(!($this->conn_slave =mysql_connect($arr[0],$arr[1],$arr[2])))
{
$this->ErrorMsg("Can't Connect MySQL Server(".$arr[0].")!");
return false;
}
$this->dbhash ='slave_'.$this->db_database;
}
if(!(mysql_select_db($this->db_database, $this->conn_slave)))
{
$this->ErrorMsg("Can't Select MySQL Server Database(".$arr[0].")!");
return false;
}
mysql_query("SET NAMES 'UTF8'");
}
}
//-----------------------------------------------------------------
// 处理表名称
function parseSql($sql)
{
if (defined('DB_PREFIX') && DB_PREFIX!='')
{
$tab_prefix=DB_PREFIX;
$sql=preg_replace("/\s+FROM\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", " FROM `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/INSERT\s+into\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "insert into `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/SHOW\s+TABLE\s+LIKE\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "show tables like `{$tab_prefix}\\1`", $sql);
$sql=preg_replace("/INSERT\s+ingore\s+into\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "insert ingore into `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/UPDATE\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "update `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/UPDATE\s+ingore\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "update ingore `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/CREATE\s+TABLE\s+\`?\[?([\w\-\_]+)\]?\`?\s+like\s+\`?\[?([\w\-\_]+)\]?\`?/i", "create table `\\1` like `{$tab_prefix}\\2`", $sql);
$sql=preg_replace("/CREATE\s+TABLE\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "create table `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "create table if not exists `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/DROP\s+TABLE\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "drop table `{$tab_prefix}\\1` ", $sql);
$sql=preg_replace("/DROP\s+TABLE\s+IF\s+EXISTS\s+\`?\[?([\w\-\_]+)\]?\`?\s+/i", "create table if exists `{$tab_prefix}\\1` ", $sql);
}
return $sql;
}
//-----------------------------------------------------------------
// 释放查询结果
function db_free()
{
if(extension_loaded('mysqli'))
{
if(is_object($this->queryID)){
$this->queryID->free_result();
}
$this->queryID = null;
}
else
{
if(is_object($this->queryID))
{
mysql_free_result($query);
}
$this->queryID = null;
}
}
//-----------------------------------------------------------------
// sql 安全处理
function escapeSQL($str, $like = FALSE)
{
$search = array('/union(\s*(\/\*.*\*\/)?\s*)+select/i', '/load_file(\s*(\/\*.*\*\/)?\s*)+\(/i', '/into(\s*(\/\*.*\*\/)?\s*)+outfile/i');
$replace = array('union select', 'load_file (', 'into outfile');
if (is_array($str))
{
foreach ($str as $key => $val)
{
$str[$key] = $this->escapeSQL($val, $like);
}
str_replace(array('\n', '\r'), array(chr(10), chr(13)), preg_replace($search, $replace, $str));
return $str;
}
//
if($this->tmpConn=='master')
{
if (extension_loaded('mysqli') and function_exists('mysqli_real_escape_string') and is_object($this->conn_master))
{
$str = mysqli_real_escape_string($this->conn_master, $str);
}
elseif (function_exists('mysql_escape_string'))
{
$str = mysql_escape_string($str);
}
else
{
$str = addslashes($str);
}
}
else
{
if (extension_loaded('mysqli') and function_exists('mysqli_real_escape_string') and is_object($this->conn_slave))
{
$str = mysqli_real_escape_string($this->conn_slave, $str);
}
elseif (function_exists('mysql_escape_string'))
{
$str = mysql_escape_string($str);
}
else
{
$str = addslashes