Query 2 MySQL databases in 1 statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • noor.rahman@gmail.com

    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.table A.field1 UNION
    database2.table B.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.

  • Benjamin Esham

    #2
    Re: Query 2 MySQL databases in 1 statement

    noor.rahman@gma il.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.table A.field1 UNION
    database2.table B.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



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

    Comment

    • noor.rahman@gmail.com

      #3
      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@gma il.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.table A.field1 UNION
      database2.table B.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
      >

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

      Comment

      • noor.rahman@gmail.com

        #4
        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@gma il.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.table A.field1 UNION
        database2.table B.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
        >

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

        Comment

        • noor.rahman@gmail.com

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

          Comment

          • Miguel Cruz

            #6
            Re: Query 2 MySQL databases in 1 statement

            "noor.rahman@gm ail.com" <noor.rahman@gm ail.comwrote:
            I was wondering how it may be possible to query 2 MySQL databases using
            one query statement from PHP.
            >
            For instance: SELECT database1.table A.field1 UNION
            database2.table B.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

            Comment

            • mootmail-googlegroups@yahoo.com

              #7
              Re: Query 2 MySQL databases in 1 statement

              noor.rahman@gma il.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.table A.field1 UNION
              database2.table B.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".

              Comment

              • Snef

                #8
                Re: Query 2 MySQL databases in 1 statement



                mootmail-googlegroups@ya hoo.com wrote:
                noor.rahman@gma il.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.table A.field1 UNION
                >database2.tabl eB.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?

                Comment

                • Tony Marston

                  #9
                  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
                  This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL

                  Build apps faster with Rapid Application Development using open-source RAD tools, modern RAD frameworks, and rapid application design methods.


                  <noor.rahman@gm ail.comwrote in message
                  news:1153897335 .618935.42770@m 79g2000cwm.goog legroups.com...
                  >I was wondering how it may be possible to query 2 MySQL databases using
                  one query statement from PHP.
                  >
                  For instance: SELECT database1.table A.field1 UNION
                  database2.table B.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.
                  >

                  Comment

                  • Tony Marston

                    #10
                    Re: Query 2 MySQL databases in 1 statement


                    "Snef" <s.franke@snefi t.comwrote in message
                    news:a6ed7$44c7 d63e$3ec24175$2 6825@news.chell o.nl...
                    >
                    >
                    mootmail-googlegroups@ya hoo.com wrote:
                    >noor.rahman@gma il.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.table A.field1 UNION
                    >>database2.tab leB.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
                    This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL

                    Build apps faster with Rapid Application Development using open-source RAD tools, modern RAD frameworks, and rapid application design methods.



                    Comment

                    Working...