views:

41

answers:

2

[PHP] How can I query a data from two database in one statement?

Please give me the easy way. and How to connect 2 database to use it?

Thank you

+1  A: 

It is possible to use database tables from different databases in one query, if your current connection is allowed to access both databases.

You just need to prefix every table name with the database name:

SELECT * FROM `databasename`.`tablename` ...  
... LEFT JOIN `databasename_2`.`tablename`....
Pekka
Not every table even. Just those tables that are not in currently selected database ;)
Mchl
How can I take connection to allow access both database? Give me an example
Giffary
@Giffary: this depends on what privileges have been set up on MySQL server for the user you're connecting as.
Mchl
+2  A: 

A 'database' in MySQL terms is a logical unit within a database server. To query tables from two separate databases, see Pekka's answer (though pleas note that restrictions apply - some JOINS might not work as intended etc. For more info see the MySQL docs.)

If you want to query two different database servers within the same statement, then the answer is that that isn't possible. You will have to create two separate connections, and query each of them individually.

eliego