Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 536853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:51:56+00:00 2026-05-13T09:51:56+00:00

In our last episode ( How I constructed a cross database query in MySQL

  • 0

In our last episode (How I constructed a cross database query in MySQL) I learned how to construct a cross database query in MySQL. This worked great but when our hero tried to use this newfound knowledge in PHP he found his best friend FAIL waiting for him.

I took a look at mysql_select_db for PHP. This seems to imply that if I want to use MySQL with PHP, I have a couple of choices:

  1. Use mysql_select_db but be stuck with only using one db at a time. This is our current setup and putting a database as a namespace identifier doesn’t seem to work (it works fine in the MySQL shell so I know it’s not a problem with our MySQL server setup).

  2. Don’t use mysql_select_db. From some of the examples I’ve seen, this seems to mean that I have to specify the db for every query that I make. This makes sense since I haven’t used mysql_select_db to tell PHP what db I want to access. This also makes sad since I don’t want to go through all my code and prepend a db name to every query.

Is there something better than this? Is there a way for me to do a cross db MySQL query in PHP without having to something crazy like (2)?

CLARIFICATION: None of the proposed answers actually let me do a cross db query. Instead, they allow me to access two different DBs separately. I want a solution that allows me to do something like SELECT foreign_db.login.username, firstname, lastname from foreign_db.login, user where ... NOT just make different queries to different DBs. For what it’s worth, (2) doesn’t work for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:51:56+00:00Added an answer on May 13, 2026 at 9:51 am

    You will need your databases to run on the same host.

    If so, you should be able to use mysql_select_db on your favourite/default db and manually specify a foreign database.

    $db = mysql_connect($hots, $user, $password);
    mysql_select_db('my_most_used_db', $db);
    
    $q = mysql_query("
        SELECT *
        FROM   table_on_default_db a, `another_db`.`table_on_another_db` b
        WHERE  a.id = b.fk_id
    ");
    

    If your databases run on a different host, you won’t be able to join directly. But you can then make 2 queries.

    $db1 = mysql_connect($host1, $user1, $password1);
    $db2 = mysql_connect($host2, $user2, $password2);
    
    $q1 = mysql_query("
        SELECT id
        FROM   table
        WHERE  [..your criteria for db1 here..]
    ", $db1);
    $tmp = array();
    while($val = mysql_fetch_array($q1))
        $tmp[] = $val['id'];
    
    $q2 = mysql_query("
        SELECT *
        FROM   table2
        WHERE  fk_id in (".implode(', ', $tmp).")
    ", $db2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We (apparently) had poorly executed of our Solaris MySQL database engine last night. At
In the last weeks we've reached our mysql connection limit of 1500 several times.
My last question on this same cte query was answered so quickly, I thought
Last evening I did some housekeeping on our code repository - basically moved the
Since the last week in January we've seen most of our iOS apps, and
Last Thursday we just released a new version of our website. The big change
Last year I developed a data access service for our project using Entity Framework
Our team (QA) is facing the following problem: We have a database that is
I am trying to work out a bug we've found during our last iteration
Last night one of our SQL servers developed some major problems and after a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.