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 71629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:55:04+00:00 2026-05-10T19:55:04+00:00

I have information spread out across a few databases and want to put all

  • 0

I have information spread out across a few databases and want to put all the information onto one webpage using PHP. I was wondering how I can connect to multiple databases on a single PHP webpage.

I know how to connect to a single database using:

$dbh = mysql_connect($hostname, $username, $password)          or die('Unable to connect to MySQL'); 

However, can I just use multiple ‘mysql_connect’ commands to open the other databases, and how would PHP know what database I want the information pulled from if I do have multiple databases connected.

  • 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. 2026-05-10T19:55:04+00:00Added an answer on May 10, 2026 at 7:55 pm

    Warning : mysql_xx functions are deprecated since php 5.5 and removed since php 7.0 (see http://php.net/manual/intro.mysql.php), use mysqli_xx functions or see the answer below from @Troelskn


    You can make multiple calls to mysql_connect(), but if the parameters are the same you need to pass true for the ‘$new_link‘ (fourth) parameter, otherwise the same connection is reused. For example:

    $dbh1 = mysql_connect($hostname, $username, $password);  $dbh2 = mysql_connect($hostname, $username, $password, true);   mysql_select_db('database1', $dbh1); mysql_select_db('database2', $dbh2); 

    Then to query database 1 pass the first link identifier:

    mysql_query('select * from tablename', $dbh1); 

    and for database 2 pass the second:

    mysql_query('select * from tablename', $dbh2); 

    If you do not pass a link identifier then the last connection created is used (in this case the one represented by $dbh2) e.g.:

    mysql_query('select * from tablename'); 

    Other options

    If the MySQL user has access to both databases and they are on the same host (i.e. both DBs are accessible from the same connection) you could:

    • Keep one connection open and call mysql_select_db() to swap between as necessary. I am not sure this is a clean solution and you could end up querying the wrong database.
    • Specify the database name when you reference tables within your queries (e.g. SELECT * FROM database2.tablename). This is likely to be a pain to implement.

    Also please read troelskn’s answer because that is a better approach if you are able to use PDO rather than the older extensions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have 10 years of archived sports data, spread across separate databases. Trying to
I have information stored in a database that I want to use to create
I have some information in my database like 'author', 'book' etc., that are all
i want to create 3d games for android but i don't have any information
Information: I have a site that has a design on index.php, and then you
I need to parse text files where relevant information is often spread across multiple
New to unit testing and have an app that is spread out over several
We have information in an Oracle db of type NCLOB, and I want to
I have a few sets of information available to me, currently: position on screen
Performance consideration: Spread rows in multiple tables vs concentrate all rows in one table.

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.