CodeIgniter Forums
How to use a database prefix within controllers? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to use a database prefix within controllers? (/showthread.php?tid=3895)



How to use a database prefix within controllers? - El Forum - 10-27-2007

[eluser]fancms[/eluser]
For most of my database queries I am using the Active Record Class. Occasionally I have the need to use a straightforward $this->db->query syntax, and it is here that I have issues, as I use a database prefix (ie prefix_ produces prefix_tablename).

Is there a way to access the dbprefix from the /config/Database.php class? I've tried several different methods, including $this->database->default('dbprefix')and $db['default']['dbprefix'], and needless to say, they don't work.

I'm not sure if I'm going about this the wrong way? :red:


How to use a database prefix within controllers? - El Forum - 10-27-2007

[eluser]Phil Sturgeon[/eluser]
Quite a simple on this.
Code:
// Use
$this->db->dbprefix;

// Example
$this->db->query('SELECT * FROM '.$this->db->dbprefix.$tablename);



How to use a database prefix within controllers? - El Forum - 10-27-2007

[eluser]fancms[/eluser]
Oh geez; that was embarassingly simple :red: Thank you, the pyromaniac!