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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:11:57+00:00 2026-06-03T07:11:57+00:00

My script requires connections to several databases. Some parts only need to connect to

  • 0

My script requires connections to several databases. Some parts only need to connect to one, which is all fine and dandy, however in some cases I need to perform queries on different database in a single script execution.

Currently I’m doing something like this:

function db_connect($which) {
    if($which == "main") {
        $maindb = mysqli_connect(HOSTNAME_1, USER, PASSWORD, MAIN_DB);
        return $maindb;
    } elseif($which == "stats") {
        $statsdb = mysqli_connect(HOSTNAME_2, USER, PASSWORD, STATS_DB);
        return $statsdb;
    }
}


$maindb = db_connect("main");
$statsdb = db_connect("stats");

I maintain the actual hostname, username, password and db name in a config file full of constants.

I then use the respective links on different queries.

Is there a cleaner way to do this?

  • 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-06-03T07:11:58+00:00Added an answer on June 3, 2026 at 7:11 am

    Quite good, although you shoul try not to duplicate your code :

    function db_connect($which) {
        if($which == "main") {
            $host = HOSTNAME_1;
            $db = MAIN_DB;
        } elseif($which == "stats") {
            $host = HOSTNAME_2;
            $db = STATS_DB;
        } else {
            throw new Exception('unknown db');
        }
        return mysqli_connect($host, USER, PASSWORD, $db);
    }
    
    
    $maindb = db_connect("main");
    $statsdb = db_connect("stats");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a script which requires some stuff to be carried out in an
I have a system for a file hosting site which requires a PHP script
I'm trying to run multiple instances of the same script in AutoIt which requires
I have a script that requires ~5 minutes to execute and complete. Everything takes
I have a Perl script that requires a couple of plugins, for istance nmap.
I am currently developing a script that requires images stored into Mongo with GridFS,
I get this error when trying ruby script/console Rails requires RubyGems >= . Please
I am creating a script to configure and launch software. My current application requires
In a bash script, I'm using wine to call a DOS program that requires
Okay, I am developing an Application which requires getting sensitive information from a database

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.