Welcome Guest, Not a member yet? Register   Sign In
how to access database.php $db[ x][ x] values from a controller
#1

[eluser]sramos[/eluser]
hi codeigniter fans!

im trying to access the

Code:
$db['default']['hostname'];
$db['default']['username'];
$db['default']['password'];
$db['default']['database'];

values in database.php from a controller, how do you do this ?
Ive seen that the config.php properties are available through the use of:
Code:
$this->config->config(xxx);

-thanks!
#2

[eluser]davidbehler[/eluser]
There is no built-in way to do this...it's nothing you normally need.
What you could do is something like this:
Code:
include(APPPATH.'config/database.php'); //maybe you have to add a slash before config..not sure if APPPATH has a slash at the end

echo $db['default']['hostname'];
#3

[eluser]WanWizard[/eluser]
And once the database driver is loaded you can access them using $this->db.
#4

[eluser]n0xie[/eluser]
This is part of my DB_Sync Lib:

Code:
class DB_Sync {

    protected $dbconfig;    // stores the database array

    function __construct()
    {
        include APPPATH .'config/database.php';

        $this->dbconfig = $db;
    }

    /**
     * Returns the database array or a database group
     *
     * @param string optional $group name of the database group to target
     * @return array
     */
    function get_config($group = NULL)
    {
        if (is_null($group))
        {
            return $this->dbconfig;
        }

        return (isset($this->dbconfig[$group])) ? $this->dbconfig[$group] : FALSE;
    }
}

This way you don't have to worry about scoping issues and it looks a bit more elegant.

Code:
$this->db_sync->get_config(); // returns the whole database array
$this->db_sync->get_config('default'); // returns the 'default' group
#5

[eluser]sramos[/eluser]
thanks guys!

looking at the CI_DB_mysql_driver, i found a way to do it:
Code:
$this->load->database();

$this->db->hostname;
$this->db->username;
$this->db->password;
$this->db->database;
#6

[eluser]Unknown[/eluser]
[quote author="sramos" date="1282595092"]thanks guys!

looking at the CI_DB_mysql_driver, i found a way to do it:
Code:
$this->load->database();

$this->db->hostname;
$this->db->username;
$this->db->password;
$this->db->database;
[/quote]

Thanks. Couldn't find this in the documentation, saved me the effort of creating a DB_Sync library like n0xie's. Smile




Theme © iAndrew 2016 - Forum software by © MyBB