ZPF MySQL database layer multi-instance implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.