We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Using DB in Extension

Development and Programming

emorling's avatar
emorling
66 posts
14 years ago
emorling's avatar emorling

I need to connect to another database, using another user in an extension. How do I go about doing that? Thanks

I currently use this, can I create another connection?

$this->EE =& get_instance();
$query = $this->EE->db->get('other_database.characters');

Moved to Development and Programming by Moderator

       
Sue Crocker's avatar
Sue Crocker
26,054 posts
14 years ago
Sue Crocker's avatar Sue Crocker

Hi, emorling.. sounds like this belongs in the Development forum.. moving there for additional community support.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

To get this working, first you need to go to the system/expressionengine/config/database.php file (depending on your setup it might be somewhere else) and add the other database connection information to it. Example:

//standard databases connection information
$db['expressionengine']['hostname'] = "localhost";
$db['expressionengine']['username'] = "db_username";
$db['expressionengine']['password'] = "db_password";
$db['expressionengine']['database'] = "db_name";
$db['expressionengine']['dbdriver'] = "mysql";
$db['expressionengine']['dbprefix'] = "exp_";
$db['expressionengine']['pconnect'] = FALSE;
$db['expressionengine']['swap_pre'] = "exp_";
$db['expressionengine']['db_debug'] = TRUE;
$db['expressionengine']['cache_on'] = FALSE;
$db['expressionengine']['autoinit'] = FALSE;
$db['expressionengine']['char_set'] = "utf8";
$db['expressionengine']['dbcollat'] = "utf8_general_ci";
$db['expressionengine']['cachedir'] = "/expressionengine/cache/db_cache/";

//notice the array index of secondary, we will use this later...
$db['secondary']['hostname'] = "localhost";
$db['secondary']['username'] = "db_username";
$db['secondary']['password'] = "db_password";
$db['secondary']['database'] = "db_name";
$db['secondary']['dbdriver'] = "mysql";
$db['secondary']['dbprefix'] = ""; // <---- Make sure this is empty or set to another prefix if you are using one
$db['secondary']['pconnect'] = FALSE;
$db['secondary']['swap_pre'] = ""; // <---- Make sure this is empty or set to another prefix if you are using one
$db['secondary']['db_debug'] = TRUE;
$db['secondary']['cache_on'] = FALSE;
$db['secondary']['autoinit'] = FALSE;
$db['secondary']['char_set'] = "utf8";
$db['secondary']['dbcollat'] = "utf8_general_ci";
$db['secondary']['cachedir'] = "/expressionengine/cache/db_cache/";

Now lets assume we are inside of a plugin and we want to use this secondary database connection:

$this->EE = get_instance();

//get entries form main EE db
$entries = $this->EE->db->from('channel_titles')->get();

/*
    Things to note:
    1. We are using the secondary index we specified in the database.php file
    2. And we are passing TRUE as the second parameter
    3. Lastly we are assigning the new database connection to the variable $secondary_db
*/
$secondary_db = $this->EE->load->database('secondary', TRUE);
    
//get people from the secondary db
$people = $secondary_db->from('people')->get();

//... other cool code ....
       
emorling's avatar
emorling
66 posts
14 years ago
emorling's avatar emorling

Thanks

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.