0

Is it possible to join 2 different databases in mysql using php?

for instance:

$sql = "SELECT * FROM db1.table LEFT JOIN db2.table USING (id)";    
$result = mysql_query($sql);    
--

I know how to create multiple new different database connections using php. But I'm unable to figure out if it's possible to acutally join two different databases in mysql using php in one query.

Matthew
  • 9,851
  • 4
  • 46
  • 77
Random5000
  • 1,562
  • 3
  • 16
  • 26
  • 4
    Have you tried using the code you gave? If so, what error messages or other problems are you having with it? – icktoofay Aug 25 '13 at 06:48
  • 1
    Please don't use mysql_* as it's deprecated, use mysqli_ or PDO instead. ref to http://php.net/manual/en/book.mysqli.php – Amit Malakar Aug 25 '13 at 06:49
  • Yes you can do that, I do that all the time. Why you asked before trying? – Havenard Aug 25 '13 at 07:07
  • @RC. lol its dup and we can not cast close vote :D – NullPoiиteя Aug 25 '13 at 07:08
  • @NullPoiиteя yup I was reading about the issue on meta ;) –  Aug 25 '13 at 07:09
  • Thanks, it works, for some reason I always thought because we were connecting to a single database when we made the connection it wouldn't work. I appreciate the responses for this question, i did try and search though, thanks again! – Random5000 Aug 30 '13 at 08:04

1 Answers1

0

I believe you can do it simply like:

SELECT DB1Table.columnIWant, DB2Table.ColumnIWant
FROM DB1.AppropiateTable as DB1Table
JOIN DB2.TableToJoin as DB2Table
ON DB1Table.ID = DB2Table.ID

Assuming the user is auth'ed for both tables. Also - see this link on multiple connections:

How do you connect to multiple MySQL databases on a single webpage?

Community
  • 1
  • 1
Matthew
  • 9,851
  • 4
  • 46
  • 77