/**
- * MySQL read/write splitting
- * $ Db_config = array (
- * 'Master' => array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
- * 'Slave '=> array (
- * Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
- * Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat ')
- *)
- *);
- *
- * Note: If there are multiple slave instances, one of them is randomly connected.
- * Last edit: bbs.it-home.org
- */
- /*
- $ Db_config = array (
- 'Master' => array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '123 ', 'DB' => 'Stat '),
- 'Slave '=> array (
- Array ('host' => 'localhost: 100', 'user' => 'admin', 'passwd' => '2016 ', 'DB' => 'Stat '),
- Array ('host' => 'localhost: 808080', 'user' => 'admin', 'passwd' => '123', '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; // database connection instance
- Private static $ _ master = null; // the master database connects to the instance.
- Private static $ _ slave = null; // reconnect to the instance
-
- Public $ _ config = array (); // database connection configuration information
- Public $ _ res = null; // query the instance handle
- Public $ _ flag = ''; // identifies whether the current statement is executed on the master or database.
- 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;
- }
/**
- * Obtain 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 name of the connected Database. by default, the database of the configuration file is connected.
- * @ Param $ mode rw indicates connection to the master database, and r-w indicates read/write splitting.
- */
- 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 master 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 the 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;
- }
-
- /**
- * Obtain a single row record
- * Enter description here...
- * @ Param mixed $ rs
- */
- Public function get ($ rs = ''){
- If (empty ($ rs )){
- $ Rs = $ this-> _ res;
- }
- Return mysql_fetch_row ($ rs );
- }
-
- /**
- * Obtain multiple rows of 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;
- }
/**
- * Obtain the number of rows affected by the previous statement.
- * Enter description here...
- */
- Public function fetchNum (){
- Return mysql_affected_rows ($ this-> _ link );
- }
-
- /**
- * Destructor to release database connection resources
- * Enter description here...
- */
- Public function _ destruct (){
- Mysql_close ($ this-> _ link );
- }
- }
|