- 论坛徽章:
- 0
|
个人觉的是比较合理的一种模式了,兼顾了维护与运营。
- class DBLink {
- var $con;
- var $user;
- var $password;
- var $masterDB;
- var $slaveDB;
- var $useDB;
-
- function DBLink() {
- $this->user="xxx";
- $this->password="xxx";
- $this->masterDB="192.168.0.1";
- $this->slaveDB=array("192.168.0.2","192.168.0.3");
- //register_shutdown_function(array( &$this, "CloseDB" )); //for php5
- }
-
- function MasterDB() {
- $this->useDB=$this->masterDB;
- $this->con=mysql_connect($this->useDB,$this->user,$this->password);
- register_shutdown_function(array( &$this, "CloseDB" ));
- return $this->con;
- }
-
- function SlaveDB() {
- $this->useDB=$this->slaveDB[array_rand($this->slaveDB)];
- $this->con=mysql_connect($this->useDB,$this->user,$this->password);
- register_shutdown_function(array( &$this, "CloseDB" ));
- return $this->con;
- }
-
- function CustomDB($db) {
- $this->useDB=$db;
- $this->con=mysql_connect($this->useDB,$this->user,$this->password);
- register_shutdown_function(array( &$this, "CloseDB" ));
- return $this->con;
- }
-
- function CloseDB() {
- $result=mysql_close($this->con);
- $ip=explode('.',$this->useDB);
- if ($result) {
- print "<!-- {$ip[3]} closed! -->";
- } else {
- print "<!-- {$ip[3]} close failed! -->";
- }
- return $result;
- }
- }
复制代码
[ 本帖最后由 枫叶红了 于 2006-1-20 18:37 编辑 ] |
|