March 31, 2015 18:27:34
Scenario: A request may be linked to a number of different databases, such as Master and slave, in order to avoid repeating the connect database, you can save the linked database objects
The next time it's called, just take it out and use it.
Realize:
1 classMySQLdb2 {3 //Multi-Host instance4 Private Static $instance=Array();5 //Connection6 Public $link=NULL;7 8 Public $_database= ";//Current database name9 Public $_tablename= ";//table name of the current tableTen Public $_dt= ";//Database.tablename One A - //If the host is not changed and a MySQL connection already exists, no new connections will be created - //If the host changes, it is regenerated into an instance to create a connection the Public Static functionGetInstance ($host,$username,$password,$database,$tablename) - { - if(Empty(Self::$instance[$host])) { - $RC=NewReflectionclass (' MySQLdb '); +Self::$instance[$host] =$RC->newinstanceargs (Array($host,$username,$password,$database,$tablename)); - } + A returnSelf::$instance[$host]; at } - - Public function__construct ($host,$username,$password,$database,$tablename) - { - $this->link =NewMysqli ($host,$username,$password); - if($this->link->connect_error) { in Echo $this->link->connect_error, ' <br> '; - Exit; to } + $this->_database =$database;//Database name - $this->_tablename =$tablename;//Table name the $this->_dt = "' {$this->_database} '. ' {$this->_tablename} ' "; * $ $this->link->query ("Set Names UTF8");Panax Notoginseng } -}
Note: Implemented with the reflection feature, the constructor is public
How to use:
1 Public functionTestmysqls ()2 {3 $obj 1=$this->getlink (' Tiezi ');4 $obj 2=$this->getlink (' name ');5 6 $arr 1=$obj 1->select (', ', ', ', 1);7 $arr 2=$obj 2->select (', ', ', ', 1);8 Var_dump($arr 1,$arr 2);9}
ZPF MySQL database layer multi-instance implementation