This is a discussion on Query 2 MySQL databases in 1 statement within the PHP Language forums, part of the PHP Programming Forums category; I was wondering how it may be possible to query 2 MySQL databases using one query statement from PHP. For ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
noor.rahman@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 bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail Más sabe el diablo por viejo que por diablo. (Spanish proverb) |
|
|||
|
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 <noor.rahman@gmail.com> wrote in message news:1153897335.618935.42770@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. > |
|
|||
|
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: > noor.rahman@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 > bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail > Más sabe el diablo por viejo que por diablo. (Spanish proverb) |
|
|||
|
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: > noor.rahman@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 > bdesham@gmail.com | AIM: bdesham128 | Jabber: same as e-mail > Más sabe el diablo por viejo que por diablo. (Spanish proverb) |
|
|||
|
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. |
|
|||
|
"noor.rahman@gmail.com" <noor.rahman@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? 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 |
|
|||
|
noor.rahman@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". |
|
|||
|
mootmail-googlegroups@yahoo.com wrote: > noor.rahman@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? |
|
|||
|
"Snef" <s.franke@snefit.com> wrote in message news:a6ed7$44c7d63e$3ec24175$26825@news.chello.nl. .. > > > mootmail-googlegroups@yahoo.com wrote: >> noor.rahman@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 |