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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:00:05+00:00 2026-05-27T17:00:05+00:00

I have two databases in same server with same username and pass. Right now

  • 0

I have two databases in same server with same username and pass. Right now I am connecting to only one database, but I would like to connect to both.

For now this is my code, whick connect only to one database:

connect1.php

<?
$servername='localhost';

$dbusername='user';
$dbpassword='pass';

$dbname1='db1';
$dbname2='db2';

$link1 = connecttodb($servername,$dbname1,$dbusername,$dbpassword);
$link2 = connecttodb($servername,$dbname2,$dbusername,$dbpassword);

function connecttodb($servername,$dbname,$dbusername,$dbpassword)
{
    $link=mysql_connect ("$servername","$dbusername","$dbpassword",TRUE);
    if(!$link){die("Could not connect to MySQL");}
    mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
    return $link;
}
    ?>

I display result in result.php with this code:

<?
require "connect1.php"; 

$q=mysql_query("select * from table1 where username='test' order by id",link1);

while($nt=mysql_fetch_array($q)){
echo "$nt[location]";
}

?>

I would like to display similar data in result.php, but with connection to db2

How can I do that? Thank you!

  • 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-27T17:00:06+00:00Added an answer on May 27, 2026 at 5:00 pm

    Instead of having a single global $link variable, you need two:

    $link1 = connecttodb($servername1,$dbname1,$dbusername1,$dbpassword1);
    $link2 = connecttodb($servername2,$dbname2,$dbusername2,$dbpassword2);
    

    And of course change connectodb() to:

    function connecttodb($servername,$dbname,$dbuser,$dbpassword)
    {
        $link=mysql_connect ("$servername","$dbuser","$dbpassword",TRUE);
        if(!$link){die("Could not connect to MySQL");}
        mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
        return $link;
    }
    

    Please note that i’ve added a fourth parameter to mysql_connect, stating TRUE for the new_link parameter (this will only be needed if the two databases reside on the same server).

    Then, for each query you will have to specify the corresponding link variable (either $link1 or $link2) according to the database you wish to query.

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

Sidebar

Related Questions

I have a table and two databases which have the same table, but one
I have two databases same server same domain and same username and password. I
I have two databases called DB1 and DB2 on the same database server. I
I have two tables in different databases on the same database server. Both the
I have two databases on the same SQL Server: Database A table 1 table
I have two SQL Server (2000) databases. Both are used for the same project,
I have two databases at the same server 192.168.1.100 DB1 and DB2 When I'm
I have two databases with same structure in MS SQL server. I'd like to
I have two databases, one MySQL 5, one SQL Server 2000. I've got the
I have two databases on the same server with the same name and different

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.