13

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?

3

6 Answers 6

58

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
Sign up to request clarification or add additional context in comments.

1 Comment

how to get all config defined in database.php at once?
9

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

7

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

2

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

-2

You should be able to get at your configuration setting like this :

$this->config['env']

1 Comment

I downvoted this because it didn't work at all for me...did it work in a previous version of CodeIgniter or something?
-3

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

$this->config->item('item name');

5 Comments

Except it's not a config item. Config items are stored in the $config array. I'm trying to access database configuration stored in $db array.
Try echo $this->db->database;
I've seen that on the net and tried it. Doesn't work. Wondering if it's a version thing. I'm using latest CI.
Can you try $this->config->item('username', 'database'); ?
echo $this->db->hostname; for example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.