我有 2 个数据库服务器 DRC 和生产服务器..
我将 php 配置文件与生产服务器 ip 连接以连接其数据库(它工作正常),如果生产数据库未连接、未选择或删除,则我做了一个 if 语句,然后去连接 DRC 服务器以获取其数据库.
下面的代码使此操作可以正常切换本地主机上的 2 个数据库,但如果生产数据库不可用,则它不会连接到 DRC 数据库。
//Production constants
defined ('DB_SERVER')? null : define ("DB_SERVER" , "20.20.10.1");
defined ('DB_USER') ? null : define ("DB_USER" , "root");
defined ('DB_PASS') ? null : define ("DB_PASS" , "rootpass");
defined ('DB_NAME') ? null : define ("DB_NAME" , "dbname");
//DRC constants
defined ('DB_SERVER_DRC')? null : define ("DB_SERVER_DRC" , "20.20.10.2");
defined ('DB_USER_DRC') ? null : define ("DB_USER_DRC" , "root");
defined ('DB_PASS_DRC') ? null : define ("DB_PASS_DRC" , "rootpass");
defined ('DB_NAME_DRC') ? null : define ("DB_NAME_DRC" , "dbname");
function __construct() {
$this->open_connection();
}
public function open_connection() {
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
if (!$this->connection) {
$this->open_connection_DRC();
} else {
$db_select = mysql_select_db(DB_NAME, $this->connection);
if (!$db_select) {
$this->open_connection_DRC();
}
}
}
public function open_connection_DRC() {
$this->connection = mysql_connect(DB_SERVER_DRC, DB_USER_DRC, DB_PASS_DRC);
if (!$this->connection) {
die("Database connection failed: " . mysql_error());
} else {
$db_select = mysql_select_db(DB_NAME_DRC, $this->connection);
if (!$db_select) {
die("Database selection failed: " . mysql_error());
}
}
}
任何解决方案或其他方式,等待您的积极回应。