-
-
/** - * MySQL read-write separation class
- * $db_config = array(
- * 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'= >'123456','db'=>'stat'),
- * 'slave' => array(
- * array('host'=>'localhost:3307','user'=>' admin','passwd'=>'123456','db'=>'stat'),
- * array('host'=>'localhost:3308','user'=>'admin', 'passwd'=>'123456','db'=>'stat')
- * )
- * );
- *
- * Note: If there are multiple slaves, randomly connect one of them
- * Last edit: bbs .it-home.org
- */
- /*
- $db_config = array(
- 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>'stat'),
- 'slave' => array(
- array('host'=>'localhost:3307','user'=>'admin','passwd'=>'123456','db'=>'stat'),
- array('host'=>'localhost:3308','user'=>'admin','passwd'=>'123456','db'=>'stat')
- )
- );
$db = MySQL::getInstance('','r-w');
$sql = "select * from admin";
$rs = $db->query($sql);
- while ($row = $db->fetch($rs)){
- echo "uid:".$row['uid']." ".$row['userName']."
";
- }
echo " ";
- */
class MySQL
- {
- private static $_instance = null;//数据库连接实例
- private static $_master = null;//主数据库连接实例
- private static $_slave = null;//重数据库连接实例
-
- public $_config = array();//数据库连接配置信息
- public $_res = null;//查询实例句柄
- public $_flag = '';//标识当前语句是在主还是重数据库上执行
- public $_link = null;
-
- /**
- * Single instance
- * Enter description here ...
- * @param unknown_type $dbname
- * @param unknown_type $mode
- */
- public static function & getInstance($dbname='',$mode='rw'){
- if (is_null(self::$_instance)){
- self::$_instance = new self();
- self::$_instance->__getConf();
- self::$_instance->connect($dbname,$mode);
- }
-
- return self::$_instance;
- }
/**
- * Get database configuration information
- * Enter description here...
- */
- public function __getConf(){
- global $db_config;
- $this->_config['master'] = $db_config['master'];
- $this->_config['slave'] = $db_config['slave'];
- }
-
- /**
- * Database connection
- * Enter description here...
- * @param $dbname specifies the database name to connect to. By default, the library of the configuration file is connected
- * @param $mode rw means connecting to the main library, r-w means reading and writing separation
- */
- public function connect($dbname='',$mode = 'rw'){
- if($mode == 'rw'){
- if(is_null(self::$_master)){
- $this->_master = $this->_slave = $this->conn_master($dbname);
- }
- }else{
- if(is_null(self::$_master)){
- $this->_master = $this->conn_master($dbname);
- }
- if(is_null(self::$_slave)){
- $this->_slave = $this->conn_slave($dbname);
- }
- }
- }
-
- /**
- * Connect to the main database server
- * Enter description here...
- */
- public function conn_master($dbname=''){
- $_link = mysql_connect($this->_config['master']['host'],$this->_config['master']['user'],$this->_config['master']['passwd'],true) or die ("Connect ".$this->_config['master']['host']." fail.");
- mysql_select_db(empty($dbname)?$this->_config['master']['db']:$dbname,$_link) or die(" The DB name ".$this->_config['master']['db']." is not exists.");
- mysql_query("set names utf8",$_link);
- return $_link;
- }
-
- /**
- * Connect to slave database server
- * Enter description here...
- */
- public function conn_slave($dbname=''){
- $offset = rand(0,count($this->_config['slave'])-1);
- $_link = @mysql_connect($this->_config['slave'][$offset]['host'],$this->_config['slave'][$offset]['user'],$this->_config['slave'][$offset]['passwd'],true) or die(" Connect ".$this->_config['slave'][$offset]['host']." fail.");
- mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$_link) or die(" The DB name ".$this->_config['slave'][$offset]['db']." is not exists.");
- mysql_query("set names utf8",$_link);
- return $_link;
- }
/**
- * Execute database query
- * Enter description here ...
- * @param string $sql
- */
- public function query($sql,$master=true){
if($master == true || (substr(strtolower($sql),0,6) != 'select') && $master == false){
- $this->_res = mysql_query($sql,$this->_master);
- if(!$this->_res){
- $this->_error[] = mysql_error($this->_master);
- }
- $this->_flag = 'master';
- $this->_link = $this->_master;
- } else {
- $this->_res = mysql_query($sql,$this->_slave);
- if(!$this->_res){
- $this->_error[] = mysql_error($this->_slave);
- }
- $this->_flag = 'slave';
- $this->_link = $this->_slave;
- }
return $this->_res;
- }
-
- /**
- * Get a single row of records
- * Enter description here ...
- * @param mixed $rs
- */
- public function get($rs=''){
- if(empty($rs)){
- $rs = $this->_res;
- }
- return mysql_fetch_row($rs);
- }
-
- /**
- * Get multi-line records
- * Enter description here ...
- * @param mixed $rs
- * @param $result_type
- */
- public function fetch($rs = ''){
- if(empty($rs)){
- $rs = $this->_res;
- }
- return mysql_fetch_array($rs,MYSQL_ASSOC);
- }
-
- /**
- * Insert data
- * Enter description here ...
- * @param unknown_type $sql
- */
- public function add($sql){
- $rs = $this->query($sql);
- if($rs)
- return mysql_insert_id($this->_link);
- return false;
- }
-
- /**
- * Update data
- * Enter description here ...
- * @param unknown_type $sql
- */
- public function update($sql){
- if(empty($sql)) return false;
- $rs = $this->query($sql);
- if($rs)
- return $this->fetchNum();
- return false;
- }
/**
- * Get the number of rows affected by the previous statement
- * Enter description here...
- */
- public function fetchNum(){
- return mysql_affected_rows($this->_link);
- }
-
- /**
- * Destructor, releases database connection resources
- * Enter description here...
- */
- public function __destruct(){
- mysql_close($this->_link);
- }
- }
-
复制代码
|