-
-
/**
- * 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有多個時隨機連接其中的一個
- * 最後編輯:bbs.it-home.org
- */
- /*
- $db_config = array(
- 'master' => array('host'=>'localhost:3306','user'=>'admin','passwd'=>'123456','db'=>' stat'),
- '從站' => 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');< ;/p>
$sql = "從管理中選擇*";
$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;
-
- /**
- * 單一實例
- * 在此輸入描述...
- * @paramknown_type $dbname
- * @paramunknown_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[🎜> 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) 或死亡("連線".$this->_config['master']['host']." 失敗。 ");
- mysql_select_db(empty($dbname)?$this->_config ['master']['db']:$dbname,$_link) 或die(" 資料庫名稱".$this->_config[ 'master']['db']." 不存在。") ;
- mysql_query("設定名稱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) 或die(" Connect ".$this->_config['slave'][$offset][' host']." 失敗。");
- mysql_select_db(empty($dbname)?$this->_config['slave'][$offset]['db']:$dbname,$ _link) 或die( " 資料庫名稱".$this->_config['slave'][$offset]['db']." 不存在。");
- mysql_query("設定名稱utf8" ,$_link);
- return $_link;
- }
/**
- * 執行資料庫查詢
- * Enter description here ...
- * @param string $sql
- */
- 公用函數查詢($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
- /
- * 更新資料
- * 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 ...
- */
- 公用函數更新($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);
- }
-
- /***/* 🎜> public function __destruct(){
- mysql_close($this->_link);
- }
- }
-
-
複製代碼
|