How to access DB configuration in CodeIgniter?

In my application, I need to know what values ​​are assigned to DB configuration items such as database , username , etc. How do I access this information?

+7
source share
6 answers

You can get it with this: http://codeigniter.com/user_guide/libraries/config.html

 $this->config->item('item name'); 
+1
source

I don't have enough comments to comment on Matt Brown's answer, but just adding a bit if anyone forgets ...

first download the db driver as follows:

 $this->load->database(); 

then you can easily access what you need:

 $this->db->hostname $this->db->username $this->db->password $this->db->database 
+35
source

Almost all configuration values ​​are available through $ this-> db (look at system / database / DB_driver.php).

This is what worked for me ... no other suggestions were here.

+9
source

If you have several database connection groups defined in config / database.php, for example:

  $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 parameters of any particular db in the controller or model:

  $db = $this->load->database('dbname'); 

If you want to use in the helper or library:

  $ci = &get_instance(); $db = $ci->load->database('dbname'); 

Connection parameters will be available in the form of $ db-> hostname, $ db-> username, etc.

+5
source

I stumbled upon this, looking for a way to find all of the database settings. Could not find the solution online, but found useful code in system/database/DB.php

Here is my approach, get the contents of the entire database configuration:

  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']; } 
0
source

You should be able to configure the settings as follows:

 $this->config['env'] 
-one
source

All Articles