<?php header("Content-Type:text/html; charset=utf8"); /** * MySQL读写分离类的实现 * $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') * ) * ); * * 注释:如果slave有多个时随机连接其中的一个 */ $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 call_id,caller,callee,call_time,call_money,`date`,media_money,record_money FROM `success_bill` WHERE user_id='1' AND `date` BETWEEN 1427081035 AND 1427089577 LIMIT 0,15 "; $start_time = microtime(true); $rs = $db->query($sql); while ($row = $db->fetch($rs)){ echo "".$row['call_id']." ".$row['caller']." ".$row['callee']." ".$row['call_time']." ".$row['call_money']." ".$row['date']." ".$row['media_money']." ".$row['record_money']." "; } // var_dump($db->get($rs)) ; $end_time = microtime(true); echo "总耗时 ", (($end_time - $start_time)*1000), " MS\n"; echo "峰值内存 ", round(memory_get_peak_usage()/1000), " KB\n"; echo "<hr />"; 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; /** * 单实例 * 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; } /** * 获取数据库配置信息 * Enter description here ... */ public function __getConf(){ global $db_config; $this->_config['master'] = $db_config['master']; $this->_config['slave'] = $db_config['slave']; } /** * 数据库连接 * Enter description here ... * @param $dbname 指定连接的数据库名,默认情况下连接配置文件的库 * @param $mode rw表示连接主库,r-w表示读写分离 */ 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); } } } /** * 连接到主数据库服务器 * 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; } /** * 连接到从数据库服务器 * 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; } /** * 执行数据库查询 * 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; } /** * 获取单行记录 * Enter description here ... * @param mixed $rs */ public function get($rs=''){ if(empty($rs)){ $rs = $this->_res; } return mysql_fetch_row($rs); } /** * 获取多行记录 * 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); } /** * 插入数据 * 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; } /** * 更新数据 * 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; } /** * 获取上一条语句影响的行数 * Enter description here ... */ public function fetchNum(){ return mysql_affected_rows($this->_link); } /** * 析构函数,释放数据库连接资源 * Enter description here ... */ public function __destruct(){ mysql_close($this->_link); } }