【发布时间】:2014-09-23 13:37:19
【问题描述】:
我有这样的代码,其中$_DB_HOST1 != $_DB_HOST2:
$dbPnt1 = new Database($_DB_HOST1, $_DB_SCHEMA1, $_DB_USER1, $_DB_PASS1);
$dbPnt2 = new Database($_DB_HOST2, $_DB_SCHEMA2, $_DB_USER2, $_DB_PASS2);
if($dbPnt1->connect())
{
if($dbPnt2->connect())
{
echo "SUCCESS";
}
else
echo "ERROR 2";
}
else
echo "ERROR 1";
Database 类的结构如下:
class Database
{
// ...
public function __construct($host, $schema, $username, $password)
{
$this->host = $host;
$this->schema = $schema;
$this->username = $username;
$this->password = $password;
}
public function connect()
{
if(!$this->connected)
{
$this->link = mysqli_connect($this->host,$this->username,$this->password);
if($this->link)
{
$this->db = mysqli_select_db($this->link, $this->schema);
if($this->db)
{
$this->connected = true;
return true;
}
else
return false;
}
else
return false;
}
else
return true;
}
// ...
}
问题是我似乎无法在同一个 PHP 脚本中连接到 2 个不同的数据库主机。我的代码中是否有我没有看到的错误?
谢谢
【问题讨论】:
-
它的反应是什么?用户名/密码/主机/数据库名称是否正确?
-
它看起来应该对我有用。