我有一个在线数据库,我需要连接我的本地进行一些数据同步。 所以我做的是使用本地IP地址为主机设置远程MYSQL。现在,让这行代码连接到本地和在线数据库:
$connection = mysql_connect('localhost', 'root');
if(!$connection) {
die ("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db('db_name', $connection);
if(!$db_select) {
die("Database connection failed: ". mysql_error());
}
/* connection to online database */
$connection_online = mysql_connect(localhost, DB_USER_ONLINE, DB_PASS_ONLINE);
if(!$connection_online) {
die ("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME_ONLINE, $connection_online);
if(!$db_select) {
die("Database connection failed: ". mysql_error());
}
当我运行此代码时出现此错误: 警告:mysql_connect():在第18行的E:\ xampp \ htdocs \ capstoneProjects \ server_includes \ connection.php中拒绝用户'umalert_local'@'localhost'(使用密码:YES) 数据库连接失败:
我做对了吗?是否可以同时连接本地和在线数据库? 谢谢。
答案 0 :(得分:-1)
试试这样。
$connection = mysql_connect('localhost', 'root','root');//if db password is root.In case of null leave it like ''
if(!$connection) {
die ("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db('db_name', $connection);
if(!$db_select) {
die("Database connection failed: ". mysql_error());
}
/* connection to online database */
$connection_online = mysql_connect(localhost, DB_USER_ONLINE, DB_PASS_ONLINE);
if(!$connection_online) {
die ("Database connection failed: " . mysql_error());
}
$db_select = mysql_select_db(DB_NAME_ONLINE, $connection_online);
if(!$db_select) {
die("Database connection failed: ". mysql_error());
}