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

Connect to secondary external DB in EE3

Development and Programming

khronos's avatar
khronos
17 posts
about 8 years ago
khronos's avatar khronos

In EE2 we were able to connect to a secondary external database from within our plugins. In EE3 this doesn’t appear to work anymore. In our config.php file we have:

$config['database'] = array (
    'expressionengine' =>
        array (
          'hostname' => 'localhost',
          'username' => 'ee_user',
          'password' => 'ee_pass',
          'database' => 'ee_database',
          'db_debug' => false,
        ),
        'second_database' =>
        [
            'hostname' => 'localhost',
            'username' => 'second_user',
            'password' => 'second_pass',
            'database' => 'second_db',
            'db_debug' => false,
        ],
    );

In the plugin we have:

$db2 = ee()->load->database('second_database',true);

We get the warnings:

Illegal string offset 'hostname'
ee/legacy/database/DB.php, line 34

It appears that even though we setup everything to use a different database, EE is still connecting to the primary database. How do we fix this so we can access our second DB?

       
JT Thompson's avatar
JT Thompson
745 posts
7 years ago
JT Thompson's avatar JT Thompson

not sure why the config you posted ever worked. Try this:

$config['database'] = array (
 'expressionengine' => array (
  'hostname' => 'localhost',
  'username' => 'ee_user',
  'password' => 'ee_pass',
  'database' => 'ee_database',
  'db_debug' => false,
 ),
 'second_database' => array (
  'hostname' => 'localhost',
  'username' => 'second_user',
  'password' => 'second_pass',
  'database' => 'second_db',
  'db_debug' => false,
 ),
);
       
khronos's avatar
khronos
17 posts
7 years ago
khronos's avatar khronos

What works in EE3 for us is to have this in the database config file:

$db['expressionengine']['hostname'] = "localhost";
$db['expressionengine']['username'] = "username";
$db['expressionengine']['password'] = "password";
$db['expressionengine']['database'] = "database";
$db['expressionengine']['dbdriver'] = "mysql";
$db['expressionengine']['dbprefix'] = "exp_";
$db['expressionengine']['pconnect'] = FALSE;
$db['expressionengine']['swap_pre'] = "exp_";
$db['expressionengine']['db_debug'] = FALSE;
$db['expressionengine']['cache_on'] = FALSE;
$db['expressionengine']['autoinit'] = FALSE;
$db['expressionengine']['char_set'] = "utf8";
$db['expressionengine']['dbcollat'] = "utf8_general_ci";
$db['expressionengine']['cachedir'] = "/path/to/eecms/cache/db_cache/";

$db['second_database']['hostname'] = "localhost";
$db['second_database']['username'] = "username2";
$db['second_database']['password'] = "password2";
$db['second_database']['database'] = "database2";
$db['second_database']['dbdriver'] = "mysql";
$db['second_database']['dbprefix'] = "";
$db['second_database']['pconnect'] = FALSE;
$db['second_database']['swap_pre'] = "";
$db['second_database']['db_debug'] = true;
$db['second_database']['cache_on'] = FALSE;
$db['second_database']['autoinit'] = FALSE;
$db['second_database']['char_set'] = "utf8";
$db['second_database']['dbcollat'] = "utf8_general_ci";
$db['second_database']['cachedir'] = "/path/to/eecms/cache/db_cache/";

And the following in our plugin:

$db_config = new Database\DBConfig(ee('Config')->getFile());

$db_config->getGroupConfig('second_database');
$connection = new Database\Database($db_config);
$db2 = $connection->newQuery();

$query = $db2->query('SELECT something FROM db')
       

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.