0
我在這個問題上關注了很多例子,但我似乎無法得到這個工作。 我可以單獨查詢成功的數據庫,但不能查詢同一查詢中的兩個數據庫。查詢兩個服務器上的兩個數據庫未連接
我試過別名,指的是查詢中的服務器......把名字放在名字周圍等等。
所以這是我的代碼
連接字符串:
/// 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'];
}
從查詢的輸出是這樣的
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數據類型和相同的排序規則。我不確定爲什麼會失敗。任何幫助,將不勝感激。
其實遠程主機和本地都具有相同的IP地址,但我不能讓他們通過IP唯一域進行連接。 –