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.
I use a Singleton class that I usually call
DatabaseManager. By making a static public method calledgetDB(), I never have to worry about passing the database around, as it will be available anywhere. Here’s a brief stub: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.