Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 258
Matthew Avatar asked Feb 16 '10 09:02

Matthew


2 Answers

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

like image 119
fire Avatar answered Oct 12 '22 05:10

fire


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.

like image 23
Phil Sturgeon Avatar answered Oct 12 '22 06:10

Phil Sturgeon