Query 2 MySQL databases in 1 statement

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-26-2006
noor.rahman@gmail.com
 
Posts: n/a
Default Query 2 MySQL databases in 1 statement

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.

Reply With Quote
  #2 (permalink)  
Old 07-26-2006
Benjamin Esham
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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)

Reply With Quote
  #3 (permalink)  
Old 07-26-2006
Tony Marston
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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.
>



Reply With Quote
  #4 (permalink)  
Old 07-26-2006
noor.rahman@gmail.com
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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)


Reply With Quote
  #5 (permalink)  
Old 07-26-2006
noor.rahman@gmail.com
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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)


Reply With Quote
  #6 (permalink)  
Old 07-26-2006
noor.rahman@gmail.com
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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.

Reply With Quote
  #7 (permalink)  
Old 07-26-2006
Miguel Cruz
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

"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
Reply With Quote
  #8 (permalink)  
Old 07-26-2006
mootmail-googlegroups@yahoo.com
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement

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".

Reply With Quote
  #9 (permalink)  
Old 07-26-2006
Snef
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement



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?
Reply With Quote
  #10 (permalink)  
Old 07-27-2006
Tony Marston
 
Posts: n/a
Default Re: Query 2 MySQL databases in 1 statement


"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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:10 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0