Reputation: 85
I have followed a lot of examples on this issue but I just cannot seem to get this to work. I can query the databases individually with success, but not the two in the same query.
I've tried aliasing, referring to the servers in the query... putting `` around the names etc. Nothing works.
So this is my code
Connection strings:
/// 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");
Ok then I have this
$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'];
}
The output from the Query is something like this
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`
Both join fields are INT datatypes on the join and the same collation. I am not sure why this would fail. Any help would be appreciated.
Upvotes: 0
Views: 167
Reputation: 50976
That's because your local connection can't connect to remote host directly ...
You can use 2 queries, one for each server instead
$query = mysql_query($query_string, $conn);
$query = mysql_query($query_string, $remoteconn);
Upvotes: 1