Use multiple databases in OpenCart

Use multiple databases in OpenCart

To use multiple databases in OpenCart (1.5.*), just update 3 files as given below:

  1. Config.php:
    Add:

    //New DB
    define(‘NEWDB_DRIVER’, ‘mysql’);
    define(‘NEWDB_HOSTNAME’, ‘localhost’);
    define(‘NEWDB_USERNAME’, ‘root’);
    define(‘NEWDB_PASSWORD’, ‘password’);
    define(‘NEWDB_DATABASE’, ‘sitename_newdb’);
    define(‘NEWDB_PREFIX’, ‘ndb’);

  2. Index.php:
    Below the current database setup ($db = new DB(DB_DRIVER …) add:

    $newdb = new DB(NEWDB_DRIVER, NEWDB_HOSTNAME, NEWDB_USERNAME, NEWDB_PASSWORD, NEWDB_DATABASE);
    $registry->set(‘newdb’, $newdb );

  3. system/database/mysql.php:
    Modify:

    change:
    if (!$this->link = mysql_connect($hostname, $username, $password)) {
    to:
    if (!$this->link = mysql_connect($hostname, $username, $password, true)) {




After these above steps we can access the new database by:

$this->newdb->query(“SELECT * FROM “. NEWDB_PREFIX . “users”);





Leave a Reply