In my application, I need to know what values are assigned to the DB config items such as database, username, etc. How do I access those information?
-
Need to know or need to set? codeigniter.com/user_guide/database/configuration.htmlSpaceBeers– SpaceBeers2011-12-07 09:22:33 +00:00Commented Dec 7, 2011 at 9:22
-
1No. I need to "get" the information.StackOverflowNewbie– StackOverflowNewbie2011-12-07 09:36:32 +00:00Commented Dec 7, 2011 at 9:36
-
@Rupesh Pawar's answer should workSpaceBeers– SpaceBeers2011-12-07 09:37:40 +00:00Commented Dec 7, 2011 at 9:37
6 Answers
I don't have enough rep to comment on Matt Browne's correct answer but just adding a bit incase anyone forgets...
load the db driver like so first:
$this->load->database();
then you can easily access what you need:
$this->db->hostname
$this->db->username
$this->db->password
$this->db->database
1 Comment
Pretty much all the config values are accessible via $this->db (take a look at system/database/DB_driver.php).
That's what worked for me...none of the other suggestions here did.
As an example
$config = [
'host' => $this->db->hostname,
'port' => '3306',
'username' => $this->db->username,
'password' => $this->db->password,
'database' => $this->db->database
];
For more details: https://stackoverflow.com/a/23666778/560114
Comments
In case you have multiple database connection groups defined in config/database.php, for eg :
$db['dbname']['hostname'] = "localhost";
$db['dbname']['username'] = "root";
$db['dbname']['password'] = "root";
$db['dbname']['database'] = "web_dbname";
$db['dbname_readonly']['hostname'] = "localhost";
$db['dbname_readonly']['username'] = "root";
$db['dbname_readonly']['password'] = "root";
$db['dbname_readonly']['database'] = "web_dbname_readonly";
If you want to use the connection params of any particular db in a controller or model:
$db = $this->load->database('dbname');
If you want to use in a helper or library :
$ci = &get_instance();
$db = $ci->load->database('dbname');
The connection params will be available as $db->hostname, $db->username etc.
Comments
I stumbled across this, looking for a way to find all of the DB settings. Was not able to find a solution on-line, but found some useful code in system/database/DB.php
Here's my approach, get the contents of the entire database config:
if ( ! file_exists($f = APPPATH.'config/'.ENVIRONMENT.'/database.php')
&& ! file_exists($f = APPPATH.'config/database.php'))
{
show_error('The configuration file database.php does not exist.');
}
include($f);
// Use a NEW variable.
// Because $db is a reserved name!!
$db_settings = $db;
foreach($db_settings as $key => $value) {
// .. do something with .. $this->database->load($key);
// .. do something with .. $value['database'];
// .. do something with .. $value['password'];
}
Comments
You should be able to get at your configuration setting like this :
$this->config['env']
1 Comment
You can retrieve it with this: http://codeigniter.com/user_guide/libraries/config.html
$this->config->item('item name');
5 Comments
$config array. I'm trying to access database configuration stored in $db array.