473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.

Jul 26 '06 #1
9 5832
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
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
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
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
"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
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


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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: eyes2ice | last post by:
Can I have the list of databases without to connect at to one of they? ... String DRIVER = "org.gjt.mm.mysql.Driver"; String DB_URL = "jdbc:mysql://localhost/test"; Class.forName(DRIVER);...
4
by: jy2003 | last post by:
I have read a book, which suggests we should change OR to UNION for better performance, but currently I have too many OR clauses(I have a query with 100 ORs) and it does not sound good to have 100...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
3
by: John young | last post by:
I have been looking for an answer to a problem and have found this group and hope you can assist . I have been re doing a data base I have made for a car club I am with and have been trying to...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
2
by: Bob Alston | last post by:
If you have an access form with record source being a straightforward query and where clause in the form definition, will the query be sent to the back end jet/Access database and executed there,...
1
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on...
2
by: alnoir | last post by:
I'm trying to update some records using the UPDATE and SELECT query. I have two databases. The first database (db1) is a subset of the second database (db2). However, the first database is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.