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.
Thanks.
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.
edit: removed the answer.
I misunderstood the question, my bad. ^_^
I misunderstood the question, my bad. ^_^
load up a new instance of db but pass in your custom parameters to override the ones implied from config.
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.
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.
I would just add new constants to config/site.php.
So, in config/site.php you already have
Just add...
And connect to it. config/site.php is always loaded so those constants should be available everywhere at all times.
So, in config/site.php you already have
Just add...
And connect to it. config/site.php is always loaded so those constants should be available everywhere at all times.
Thanks andrew. I will use that approach.
That is the best place, IMO.