0

I'm developing an EE module that needs to access a non EE database table that isn't prefixed with 'exp_'.

How would I go about doing this? In Codeigniter I can use set_dbprefix, but that doesn't appear to be available in EE.

I'm using EE 2.73 currently.

Thanks in advance!

2 Answers 2

1

If I recall correctly, using the query module (and relative portion of the database class) doesn't force the prefix.

https://ellislab.com/expressionengine/user-guide/add-ons/query/

So you can use that to query any full table name:

$result = ee()->db->query('SELECT * FROM not_prefixed_table')->result_array();

You just annoyingly have to write out each query.

Edit Also, I'm assuming you don't have permissions to make schema and table changes to this database, because otherwise the obvious answer is just modify the table name to include the prefix.

2
  • Thanks @jrothafer! I do have permission but I have a Codeigniter application that I've written running on the same server and I don't want to change the table names :-) Oct 7, 2015 at 13:38
  • I ended up going for this solution as it was the quickest and simplest in the end. Thanks! Oct 7, 2015 at 13:53
1

You can set the database to be used like so:

$mydb = ee()->load->database('mydatabase', TRUE);

By setting the second parameter to TRUE the function will return the database object.

and then run a query like:

$query = $mydb->select('*')->from('tableName')->get();

UPDATE:

To set the database prefix to use in the query use:

ee()->db->set_dbprefix('myprefix');
4
  • Thanks Pete! It's actually the same database, merely a non "exp_" prefixed table. Would this still work? Oct 7, 2015 at 11:50
  • Umm, I don't see why not. Just set the database to EE's database. Are you sure ee()->db->set_dbprefix doesn't work? That would be the way to do it I guess.
    – foamcow
    Oct 7, 2015 at 12:12
  • set_dbprefix gives me: Fatal error: Call to undefined method CI_DB_mysql_driver::set_dbprefix() Oct 7, 2015 at 12:34
  • That's either because you need to call in on the DB object $mydb or because EllisLab made the wonderful (being sarcastic) choice to maintain their own fork of Code Igniter instead of mirroring the open source, constantly updated version.
    – jrothafer
    Oct 7, 2015 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.