Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access database config variables from a helper in Codeigniter

Is it possible to get the database.php variables values from a helper in Codeigniter?

like image 525
razorfish Avatar asked Aug 30 '11 11:08

razorfish


2 Answers

Here is the way, normally you won't be able to use $this in helper, so you have to use get_instance(). I have given an example of 'hostname' you can use the config name you need.

   function test()
    {
        $CI =& get_instance();
        $CI->load->database();
        echo $CI->db->hostname; // give the config name here (hostname).
    }
like image 162
Muhammad Usman Avatar answered Oct 04 '22 21:10

Muhammad Usman


$ci=& get_instance();
$ci->config->load('database');
$ci->config->item('item name');

If you want to access the config file for the database when $this->config->load(); is not available, the solution could look like this:

include(APPPATH.'config/database'.EXT);
$conn = mysql_connect($db['default']['hostname'], $db['default']['username'], $db['default']['password']);

mysql_select_db($db['default']['database'], $conn);
like image 42
Tobias Avatar answered Oct 04 '22 23:10

Tobias