我在这个问题上已经有很多例子,但我似乎无法让它发挥作用。 我可以成功地单独查询数据库,但不能在同一查询中查询两个数据库。
我已经尝试了别名,指的是查询中的服务器......在名称周围加上``什么都没有。
所以这是我的代码
连接字符串:
/// LOCAL DATABASE
$databaseprefix = "w-";
$mysqlHost = 'localhost';
$mysqlUser = 'web123-USER1';
$mysqlPass = 'SOMEPASSWORD';
$mysqlDB = $databaseprefix . 'DATABASE1';
$conn = mysql_connect($mysqlHost,$mysqlUser,$mysqlPass,true) or die('Could not connect to SQL Server on '.$mysqlHost.' '. mysql_error());
///select remote database to work with
$localDB = mysql_select_db($mysqlDB, $conn) or die("x1 Couldn't open database $mysqlDB");
///////////// GET THE DATABASE WE WANT TO EDIT
$remoteHost = $_SESSION['localfolder'];
$remoteUser = $_SESSION['databaseuser'];
$remotePass = $_SESSION['databasepassword'];
$remoteDB = $_SESSION['database'];
$remoteconn = mysql_connect($remoteHost,$remoteUser,$remotePass,true) or die('Could not connect to SQL Server on '.$remoteHost.' '. mysql_error());
$clientDB = mysql_select_db($remoteDB, $remoteconn) or die("x2 Couldn't open database $remoteDB");
好的,我有这个
$query_string = "select
`$remoteHost`.`$remoteDB`.`pages`.`page_title`,
`$mysqlHost`.`$mysqlDB`.`c_users`.`name`
from `$remoteHost`.`$remoteDB`.`pages`
left join `$mysqlHost`.`$mysqlDB`.`c_users`
on `$remoteHost`.`$remoteDB`.`pages`.`created_by` = `$mysqlHost`.`$mysqlDB`.`c_users`.`user_id`";
echo $query_string;
//// Query the DB's
$query = mysql_query($query_string);
echo mysql_num_rows($query);
while($row = mysql_fetch_assoc($query)) {
echo $row['name'] . ' | ' . $row['page_title'];
}
Query的输出是这样的
select `remoteaddress.co.uk`.`remote-DB`.`pages`.`page_title`,
`localhost`.`w-DATABASE1`.`c_users`.`name`
from `remoteaddress.co.uk`.`remote-DB`.`pages`
left join `localhost`.`w-DATABASE1`.`c_users` on
`remoteaddress.co.uk`.`remote-DB`.`pages`.`created_by` = `localhost`.`w-DATABASE1`.`c_users`.`user_id`
两个连接字段都是连接上的INT数据类型和相同的排序规则。我不知道为什么会失败。任何帮助将不胜感激。
答案 0 :(得分:1)
那是因为您的本地连接无法直接连接到远程主机......
您可以使用2个查询,而不是每个服务器一个
$query = mysql_query($query_string, $conn);
$query = mysql_query($query_string, $remoteconn);