How can I access a CodeIgniter configuration variable from a model/controller?

20,208

Solution 1

Try:

$this->load->database();
echo $this->db->dbprefix;

Normally you can use $this->config->item but I think that only allows variables set in $config

Solution 2

The documentation says you should be using:

$this->db->dbprefix('tablename');

Doesn't make a huge amount of difference but could be an easier syntax.

Share:
20,208
Matt
Author by

Matt

Updated on July 09, 2022

Comments

  • Matt
    Matt almost 2 years

    I would like to access the $db['default']['dbprefix'] variable from /application/config/database.php (CodeIgniter configuration file) from within a model so I can write my own queries using the value from the file.

    How can this be done?

  • Matt
    Matt about 14 years
    Where in the docs does it say that? I'm trying to get (not set) the name, so I don't know what advantages this offers.
  • Phil Sturgeon
    Phil Sturgeon about 14 years
    The userguide simply has bad wording on this. "If you have configured a database prefix and would like to add it in manually for, you can use the following." They mean if you want to add the prefix into your query manually, you can do this. Using $this->db->dbprefix is essentially accessing a private variable, which in PHP 4 is perfectly allowed. They have added a method for this so using it is a better idea, as if EllisLab eventually switch to full PHP 5 it will continue working even if ->dbprefix is set as an actual private property. Not massively important, but something to consider. :-)