【发布时间】:2015-05-17 15:09:33
【问题描述】:
我有两个数据库
其中一个在本地服务器上,另一个在线。
我想通过 PHP 代码在 localhost 中连接两者。
如何为此目的定义此参数?本地服务器是否需要特殊配置?
//database that is local
define( "HOST", 'localhost' ) ;
define( "DBUSER", 'root' ) ;
define( "DBNAME", 'name_db' ) ;
define( "DBPASS", '' );
//database that is online
define( "HOSTPM", 'localhost' ) ;
define( "DBUSERPM", 'username' ) ;
define( "DBNAMEPM", 'name2_db' ) ;
define( "DBPASSPM", 'password' );
$db = new PDO('mysql:host='. HOST .';dbname='. DBNAME . ';charset=utf8', DBUSER, DBPASS);
$dbpm = new PDO('mysql:host='. HOSTPM .';dbname='. DBNAMEPM . ';charset=utf8', DBUSERPM, DBPASSPM);
if (!$db) {
die('Could not connect: ' . mysqli_error());
}
if (!$dbpm) {
die('Could not connect: ' . mysqli_error());
}
【问题讨论】:
-
localhost 是您当前的机器。如果您需要访问远程资源,请通过 IP 或主机名连接到它。
-
好的!我通过 IP 和主机名测试了远程连接,但得到了类似这样的错误 Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应,或建立连接失败,因为连接的主机没有响应
-
在来 SO 之前你们会阅读文档吗?
标签: php connection