Forum Moderators: coopster
I'm looking for a solution on howto join multiple tables across databases.
This bit of code does not work.. yet..
$SqlSelectQuery =
(
"
SELECT
a.ID,
a.Name,
b.ID,
b.Name
FROM
db1.a, db2.b
WHERE
a.ID = b.ID
"
);
$SqlSelectResult = mysql_query($SqlSelectQuery, $db1);
$SqlSelectResult2 = mysql_query($SqlSelectQuery, $db2);
PHP5
MySQL5
Is it possible to join tables across multiple databases?
TIA
From the php annotated manual:
Description
resource mysql_query ( string $query [, resource $link_identifier] )
mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
[php.net...]
It is a server link and you can query any databases's on the server with that one link
I think I may be missing something here.
I'm calling two resource links that connect to two databases.
How do I merge these resource links and get mysql_query() to use them?
// Resource link, db1
$db1 = mysql_connect ("localhost", "user1", "password1") or die("Could not connect.");
mysql_select_db("db1", $db1);
// Resource link, db2
$db2 = mysql_connect ("localhost", "user2", "password2") or die("Could not connect.");
mysql_select_db("db2", $db2);
The only way i have found is to use GRANT option to change priviledges between the two users of the two databases (assuming these are different users).
I grant one user access to both databases, and then run my queries using that user
You need to use the database prefix:
$query = "SELECT a.field1, b.field1 FROM database1.table1 as a, database2.table1 as b WHERE database1.table1.field2='' AND ..... ";
Hope this makes sence.
And i hope for a better solution to exist (its frustrating to change priviledges constantly)