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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:58:39+00:00 2026-05-13T01:58:39+00:00

What is the best way to deal with the $db_conn variable once I create

  • 0

What is the best way to deal with the $db_conn variable once I create a mysqli object?

I’ve been using

$GLOBALS['_sql'] = new mysqli(...);

But this seems dirty. The alternative that I’ve seen is to be constantly passing the connection var to every function that calls it, but that’s a huge pain.

  • 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-13T01:58:39+00:00Added an answer on May 13, 2026 at 1:58 am

    I use a Singleton class that I usually call DatabaseManager. By making a static public method called getDB(), I never have to worry about passing the database around, as it will be available anywhere. Here’s a brief stub:

    class DatabaseManager
    {
        private static $instance;
        private $db_connection;
    
        public initDBConnection($connectionInfo) {
              //make connection
        }
    
        public static function getInstance()
        {
            if (self::$instance == null) {
                $className = __CLASS__;
                self::$instance = new $className();
            }
            return self::$instance;
        }
    
        public static function getDB() 
        {
             return self::getInstance()->db_connection;
        }
    
    }
    

    Once you’ve initialized a database connection, you can simply call DatabaseManager::getDB() to get the database connection.

    The beauty of this approach is that it’s easily extended to manage connections to multiple databases, as well as ensuring that you never have more than one connection open to any one database. Makes your connections very efficient.

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

Sidebar

Related Questions

I've been wondering what's the best way to deal with exceptions on CodeIgniter .
I've been trying to figure out the best way to deal with a footer
What is the best way deal with out-of-sequence Ajax requests (preferably using a jQuery)?
I am new to UI design- was wondering what the best way to deal
What is the best way to deal with batch updates using (Entity Framework) EF5?
I'm not quite sure the best way to deal with this situation. Here's a
From what I understand, the best way to deal with dates in the Zend
What is the best way to deal with utility functions in a OOP PHP
What is the best way to deal with input elements that are currently not
I wonder what is the best way to deal with 3 levels deep and

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.