By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

Query 2 MySQL databases in 1 statement

P: n/a
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Thanks.

Jul 26 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
no*********@gmail.com wrote:
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?
Take a look at some of the comments at

http://us2.php.net/manual/en/functio...-select-db.php

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
Más sabe el diablo por viejo que por diablo. (Spanish proverb)

Jul 26 '06 #2

P: n/a
The comments posted on php.net mainly addresses issues where two
SEPARATE queries accessing two different databases. This can be
accomplished with two different DB handles. However, my question is
regarding one handle accessing two databases.... as in, one query
interacting with two different databases.

Possible??

Thanks.
Benjamin Esham wrote:
no*********@gmail.com wrote:
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Take a look at some of the comments at

http://us2.php.net/manual/en/functio...-select-db.php

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
Más sabe el diablo por viejo que por diablo. (Spanish proverb)
Jul 26 '06 #3

P: n/a
The comments posted on php.net mainly addresses issues where two
SEPARATE queries accessing two different databases. This can be
accomplished with two different DB handles. However, my question is
regarding one handle accessing two databases.... as in, one query
interacting with two different databases.

Possible??

Thanks.
Benjamin Esham wrote:
no*********@gmail.com wrote:
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Take a look at some of the comments at

http://us2.php.net/manual/en/functio...-select-db.php

HTH,
--
Benjamin D. Esham
bd*****@gmail.com | AIM: bdesham128 | Jabber: same as e-mail
Más sabe el diablo por viejo que por diablo. (Spanish proverb)
Jul 26 '06 #4

P: n/a
Got it!

Apparently, if I use the database.table.feild syntax, then PHP/MySQL
connection does not seem to care which database I specified in the
mysql_select_db($DatabaseName,$db) function.

But I obviously need permissions in both the DBs.

Jul 26 '06 #5

P: n/a
"no*********@gmail.com" <no*********@gmail.comwrote:
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?
As long as the two databases are accessible via the same connection
(i.e., they are on the same server and the login credentials grant you
the required access for both of them) then it's a non-issue, it works
fine.

Otherwise it doesn't, and you will have to create a user that has the
appropriate select privileges on both databases or whatever.

miguel
--
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu
Jul 26 '06 #6

P: n/a
no*********@gmail.com wrote:
I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Thanks.

As I understand it, the database you specify when connecting to mysql
is your default database. For example, if you connect using "db1",
then to query that database, you can just use "SELECT * FROM table1"
which, because of your default database, is equivalent to "SELECT *
FROM db1.table1". You are still able to access other databases by
fully-qualifying the names, so even if "db1" is your default database,
you can still do something like "SELECT * FROM db2.table2".

Jul 26 '06 #7

P: n/a


mo*******************@yahoo.com wrote:
no*********@gmail.com wrote:
>I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Thanks.


As I understand it, the database you specify when connecting to mysql
is your default database. For example, if you connect using "db1",
then to query that database, you can just use "SELECT * FROM table1"
which, because of your default database, is equivalent to "SELECT *
FROM db1.table1". You are still able to access other databases by
fully-qualifying the names, so even if "db1" is your default database,
you can still do something like "SELECT * FROM db2.table2".
I assume that they need to be in the same MySQL?
Jul 26 '06 #8

P: n/a
When you connect to MySQL you are connecting to a server, not a database,
and a server may contain any number of databases. The reason for using the
mysql_select_db() command is to select a default database so that you do not
have to prefix each table name with a database name. It is still possible to
access a table from another database in a single query simply by using
"database.table".

HTH

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org

<no*********@gmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Thanks.

Jul 26 '06 #9

P: n/a

"Snef" <s.******@snefit.comwrote in message
news:a6***************************@news.chello.nl. ..
>

mo*******************@yahoo.com wrote:
>no*********@gmail.com wrote:
>>I was wondering how it may be possible to query 2 MySQL databases using
one query statement from PHP.

For instance: SELECT database1.tableA.field1 UNION
database2.tableB.field2.

My concern is, when connecting to MySQL (or sending a query), I only
specify 1 database connection resource ID. How does that play out when
connecting to 2 databases?

Thanks.


As I understand it, the database you specify when connecting to mysql
is your default database. For example, if you connect using "db1",
then to query that database, you can just use "SELECT * FROM table1"
which, because of your default database, is equivalent to "SELECT *
FROM db1.table1". You are still able to access other databases by
fully-qualifying the names, so even if "db1" is your default database,
you can still do something like "SELECT * FROM db2.table2".
I assume that they need to be in the same MySQL?
You mean under the same MySQL instance (server). You connect to an instance,
then create databases using that instance, and you can talk to all of those
databases within that instance. The fact that you can designate one of the
databases as the default database does not mean that you can only talk to
that database.

--
Tony Marston
http://www.tonymarston.net
http://www.radicore.org
Jul 27 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.