Single Location for db connection info

Permalink 2 users found helpful
What is the best practice for storing database connection information? I will have various Controllers connect to an external database and I don't want to include the host/user/password in each controller.php file/function view().

Thanks.

 
okhayat replied on at Permalink Reply
okhayat
config/site.php usually has all the DEFINED constants, including the main connection information.
That is the best place, IMO.
rmore replied on at Permalink Reply
Thanks, but that looks like it is for the concrete5 database not my own custom db info. I was thinking about creating a singleton class but wondering how to intergrate it into the concrete5 framework.
Brainakazariua replied on at Permalink Reply
Brainakazariua
edit: removed the answer.
I misunderstood the question, my bad. ^_^
ScottC replied on at Permalink Reply
ScottC
load up a new instance of db but pass in your custom parameters to override the ones implied from config.
Mnkras replied on at Permalink Reply
Mnkras
rmore replied on at Permalink Reply
Thanks for all the responses, but this still doesn't answer my question. The question was not how to connect to another database ( I used the webpage that Mnkras pointed out earlier ). But how to create a *SINGLE* location for db connection info. If I have 10 controllers, I don't want to embed the host/user/password in 10 locations. I was looking to implement
Recipie 10.15 from O'Reilly's PHP Cookbook...

10.15 Accessing a Database Connection Anywhere in Your Program

Problem
You’ve got a program with lots of functions and classes in it, and you want to maintain a single database connection that’s easily accessible from anywhere in the program.

Solution
Use a static class method that creates the connection if it doesn’t exist and returns the connection.
andrew replied on at Permalink Best Answer Reply
andrew
I would just add new constants to config/site.php.

So, in config/site.php you already have

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'dbuser');
define('DB_PASSWORD', 'dbpass');
define('DB_DATABASE', 'db');


Just add...

define('DB_SERVER2', 'localhost');
define('DB_USERNAME2', 'dbuser2');
define('DB_PASSWORD2', 'dbpass');
define('DB_DATABASE2', 'db2');


And connect to it. config/site.php is always loaded so those constants should be available everywhere at all times.
rmore replied on at Permalink Reply
Thanks andrew. I will use that approach.