作者alpe (薛丁格的貓)
看板PHP
標題Re: [請益] 關於php同時連接多個db
時間Wed Sep 2 21:15:16 2009
※ 引述《marcoyan (回到正軌。)》之銘言:
: 我自己回答一下好了,不過我不知這方法是不是會有問題,如果寫的不好請多指教
class Db {
private $conn;
private $res = array();
private $db_user = '';
private $db_pw = '';
private $db_host = '';
private $db_name = '';
function __construct($user, $pw, $host, $name, $conn_now = false){
$this->db_user = $user;
$this->db_pw = $pw;
$this->db_host = $host;
$this->db_name =$name;
if($conn_now) $this->connect();
}
function connect(){
$this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pw);
if(!is_resource($this->conn)) die();
return true;
}
function duery($sql, $resname = ''){
$this->res[$resname] = mysql_query($sql, $this->conn);
return is_resource($this->res[$resname])? true : false;
}
function fetchArray($resname=''){
return mysql_fetch_array($this->res[$resname]);
}
}
$db1 = new db('u1', 'pw1', 'host1', 'dbname1');
$db2 = new db('u2', 'pw2', 'host2', 'dbname2');
這樣可能比較好用
--
Live Long and Prosper
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.217.35.191
推 marcoyan :謝謝您,不過這樣不是變成在其它class裡都要先new db 09/04 14:49
→ marcoyan :另外,沒看到您的$this->dbname用到哪去了@_@ 09/04 14:51