我有一個應用程序,我想從第一個數據庫驗證用戶&管理其他數據庫的活動。 我創建了兩個類。該班的目的是在一個文件中定義:連接到兩個數據庫
$objdb1=new db1(),$objdb2=new db2();
但是,當我嘗試調用$objdb1->fn()
。它從$objdb2
&搜索顯示table1不存在?
我的第一個文件database.php中
class database
{
private $hostname;
private $database;
private $username;
private $password;
private $dblinkid;
function __construct()
{
if($_SERVER['SERVER_NAME'] == 'localhost')
{
$this->hostname = "localhost";
$this->database = "aaaa";
$this->username = "xxx";
$this->password = "";
}
else
{
$this->hostname = "localhost";
$this->database = "xxx";
$this->username = "xxx";
$this->password = "xxx";
}
$this->dblinkid = $this->connect();
}
protected function connect()
{
$linkid = mysql_connect($this->hostname, $this->username, $this->password) or die("Could not Connect ".mysql_errno($linkid));
mysql_select_db($this->database, $linkid) or die("Could not select database ".mysql_errno($linkid)) ;
return $linkid;
}
同樣第二個文件
class database2
{
private $vhostname;
private $vdatabase;
private $vusername;
private $vpassword;
private $vdblinkid;
function __construct()
{
if($_SERVER['SERVER_NAME'] == 'localhost')
{
$this->vhostname = "xxx";
$this->vdatabase = "bbbb";
$this->vusername = "xxx";
$this->vpassword = "";
}
else
{
$this->vhostname = "localhost";
$this->vdatabase = "xxxx";
$this->vusername = "xxxx";
$this->vpassword = "xxxx";
}
$this->vdblinkid = $this->vconnect();
}
protected function vconnect()
{
$vlinkid = mysql_connect($this->vhostname, $this->vusername, $this->vpassword) or die("Could not Connect ".mysql_errno($vlinkid));
mysql_select_db($this->vdatabase, $vlinkid) or die("Could not select database ".mysql_errno($vlinkid)) ;
return $vlinkid;
}
第三檔
$objdb1 = new database();
$objdb2 = new database2();
你能不能幫我在這?
問候,
潘卡
沒有足夠的代碼。告訴我們如何定義db1和db2。 – 2010-03-22 09:32:04
Mooore coooode。 – 2010-03-22 09:44:43
類的優點是可以有幾個共享相同功能但具有不同屬性的實例。在你的情況下,你只能使用一個類,並創建兩個具有不同屬性的實例(例如數據庫連接信息),因此每個數據庫都有一個。 – Gumbo 2010-03-22 13:25:43