Forum Moderators: coopster

Message Too Old, No Replies

Join on two DB

How to combine both OOP conn scripts

         

henry0

11:44 am on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This conn first DB

$db = new MySQL_Db;
$db->connect($host, $un, $pw);
$db->query($db_db);
$result = $db->query("
SELECT ........

This conn DB2

$db2 = new MySQL_Db2;
$db2->connect($host, $un, $pw);
$db2->query($db_db2);

$result = $db2->query("
SELECT.......

How will I go to combine the two conns in order to perform a join on the separated DBs

to use for ex: select * db.tb1 join db2.tb2 on tb1.id=tb2.id

vincevincevince

12:30 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"SELECT * FROM `database1`.`table1`,`database2`.`table2` WHERE ..."

i.e. prefix the table names with the database name and a .

henry0

1:15 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, but in my case how could it work
since the select is done within one or the other $result
if I do a join within DBA select...
how could I include a second select since I need also to conn with DBB

vincevincevince

1:39 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't select a DB at all, specify it all in the SQL?

henry0

2:09 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks
I have to play with it
the reason I do a select... is that it calls for the DB name
and that all my UN, PW etc.. DB details are not in sight and are kept away from root.

of course I may do that directly from that script, but I do not feel secure by having my DB details showing up in the same page/script

Is there a way to write a routine that within one select could call two Db?
I do not think so...

coopster

2:51 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You won't be able to join two different databases if you are using two different connections. The connection resource is a specific handler resource to that connection only.

However, if both databases are on the same server and the user has authority/permission to access both databases, you can then use a single connection and specify the databases in your query ...

SELECT database1.table.field1, database2.table.field2 FROM database1.table INNER JOIN database2.table ON (database1.table.primarykey = database2.table.foreignkey);

henry0

3:02 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, exactly what I was looking for
Thanks Coopster!

coopster

3:07 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome. Terminology is identifiers and qualifiers [dev.mysql.com].