Re: [請益] 關於php同時連接多個db

看板PHP作者 (薛丁格的貓)時間16年前 (2009/09/02 21:15), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《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

09/04 14:49, , 1F
謝謝您,不過這樣不是變成在其它class裡都要先new db
09/04 14:49, 1F

09/04 14:51, , 2F
另外,沒看到您的$this->dbname用到哪去了@_@
09/04 14:51, 2F
文章代碼(AID): #1Adc_eiG (PHP)
討論串 (同標題文章)
文章代碼(AID): #1Adc_eiG (PHP)