Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:39:03+00:00 2026-05-30T19:39:03+00:00

I am trying to connect to a different database than what is in my

  • 0

I am trying to connect to a different database than what is in my database config file.

I’ve been able to do this with the following in my model:

        $wp['hostname'] = "localhost";
        $wp['username'] = "root";
        $wp['password'] = "";
        $wp['database'] = "transfer";
        $wp['dbdriver'] = "mysql";
        $wp['dbprefix'] = "";
        $wp['pconnect'] = FALSE;
        $wp['db_debug'] = TRUE;
        $wp['cache_on'] = FALSE;
        $wp['cachedir'] = "";
        $wp['char_set'] = "utf8";
        $wp['dbcollat'] = "utf8_general_ci";

        $wpDB = $this->load->database($wp, TRUE);

and then running queries like so: $query = $wpDB->get();

I can only get it to work when the config values are in the model itself (so there would be a lot of duplication). I’ve tried putting the config array in the constructor, but I get an error that it can’t find it.

Where can I put the config array so I don’t have to duplicate it and that’s available throughout the model?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:39:05+00:00Added an answer on May 30, 2026 at 7:39 pm

    Database configuration usually goes in config/database.php. You can configure multiple database connections and store them with different group names:

    $active_group = 'default';
    $active_record = TRUE;
    
    $db['default']['hostname'] = 'host1';
    $db['default']['username'] = 'user1';
    $db['default']['password'] = '******';
    $db['default']['database'] = 'my_db';
    
    $db['other']['hostname'] = 'host2';
    $db['other']['username'] = 'user2';
    $db['other']['password'] = '******';
    $db['other']['database'] = 'my_other_db;
    

    The $active_group refers to the default group when the database class is loaded. To connect to another group in your model, you can use this in the model’s __construct method:

    $this->db = $this->load->database('other', TRUE);
    

    While this doesn’t play nicely with the more flexible $this->load->model('model_name', 'alias', $config) approach, it might be easier.

    More info: http://codeigniter.com/user_guide/database/connecting.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm receiving this error while I'm trying to connect to a database from a
I'm trying to follow this tutorial in order to connect to an external database
I'm trying to connect to a different machine: $this->_connection = new PDO(mysql: host=MYSQL_SERVER; dbname=MYSQL_DATABASE,MYSQL_USER,
I am trying to connect many socket.io clients for different URLs in Node.js like
I'm trying to connect to a dataset in a different project but in the
While trying to connect to a database that's set to read only, the connection
We've been seeing this problem for a while now and I'm really trying to
I'm trying to connect MySQL database and getting out datas I need it to
I'm trying to include a file with all my db connection info (hostname password
I am trying to connect to MySQL database over a network. I have installed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.