views:

1148

answers:

2

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

How can this be done?

+2  A: 

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

fire
sorted - thanks!
Matt
+1  A: 

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.

Phil Sturgeon
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.
Matt
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. :-)
Phil Sturgeon