I want to retrieve two different fields residing in different data bases. My db connection settings are this:
define ('DB_HOST', 'ipaddress1');
define ('DB_USER', 'username1');
define ('DB_PASSWORD', 'password1');
define ('DB_DATABASE', 'ecbooks');
$db_wink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, TRUE) or die("Connection Error: " . mysql_error());
mysql_select_db(DB_DATABASE) or die("Error connecting to Winkstore DB. " . mysql_error());
// DB configuration parameters : magonwink
define ('DB_REMOTE_HOST', 'ipaddress2');
define ('DB_REMOTE_USER', 'username2');
define ('DB_REMOTE_PASSWORD', 'password2');
define ('DB_REMOTE_DATABASE', 'magsonwink');
$db_magson = mysql_connect(DB_REMOTE_HOST, DB_REMOTE_USER, DB_REMOTE_PASSWORD, TRUE) or die("Connection Error: " . mysql_error());
mysql_select_db(DB_REMOTE_DATABASE, $db_magson) or die("Error connecting to magson wink DB. " . mysql_error());
define ('CMS_DB_HOST', 'ipaddress3');
define ('CMS_DB_USER', 'username3');
define ('CMS_DB_PASSWORD', 'password3');
define ('CMS_DB_DATABASE', 'mawinkcms');
$db_rp = mysql_connect(CMS_DB_HOST, CMS_DB_USER, CMS_DB_PASSWORD, true) or die("Connection Error: " . mysql_error());
mysql_select_db(CMS_DB_DATABASE, $db_rp) or die("Error connecting to DB. " . mysql_error());
but when I used this query
SELECT ecbooks.user.user_name AS field1, mawinkcms.purchase.USER_NAME AS field2 FROM ecbooks.user,mawinkcms.purchase
$result = mysql_query($query, $db_wink) or die("Couldn't execute query: " . mysql_error());
while($row = mysql_fetch_assoc($result)){
$users[] = $row;
}
I got an error could not execute the query. Thanks in advance.
Firstly, when asking for help with an error, it’s a good idea to include the error itself.
Secondly, your query doesn’t appear to have a join condition, so you will get back the cartesian product if you do get it to go – that’s probably not what you want.
Thirdly, when running a query, it runs against the database you connected to, regardless of whether you’ve connected to any others. So, in your example, you’re running a query against $db_wink; the other two connections don’t affect this at all.
For the query to work, the user who connected to $db_wink needs to have permissions on the databases ecbooks and mawinkcms, and mawinkcms must be running on the same server as ecbooks.
I’m guessing that either the permissions are not set up, or you’re running them on different databases.