Viewed   145 times


$db['default']['hostname'] = "";
$db['default']['username'] = "webuser";
$db['default']['password'] = "----";
$db['default']['database'] = "vad";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['stats']['hostname'] = "";
$db['stats']['username'] = "webuser";
$db['stats']['password'] = "---";
$db['stats']['database'] = "vad_stats";
$db['stats']['dbdriver'] = "mysql";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";

The issue is I can only define in the configuration one $active_group, default, or stats. I followed the CodeIgniter documentation and I added the following:

$DB2 = $this->load->database('stats', TRUE);

This way I connect to the second database, but I lose the connection to the first one. Does anyone have any ideas on how can I load the two database without having to do the following in all models constructors?

$database1 = $this->load->database('database1', TRUE);
$database2 = $this->load->database('database2', TRUE); 





Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.

Tuesday, October 4, 2022

I got it to work. First I edited my database.php configuration file to include the new database. Then, in my model, I declared a class variable $current_db, and I wrote the following two functions:

function getDB($db){
    if($db ==1){
        $this->current_db  = $this->load->database('default', TRUE);
    } elseif($db ==2) {
        $this->current_db  = $this->load->database('local', TRUE);

function dyno_query($table, $id){
    $query = $this->current_db->get_where($table, array('id' => $id));
    return $query->result_array();

Then in my controller, I ran the following:

$arr = array();
$arr['default'] = $this->My_Model->dyno_query('default_table', 2);
$arr['local'] = $this->My_Model->dyno_query('local_table', 74);

The result was an array with data from both databases. Maybe the key to it all is defining a class variable with a name other than $db.


To keep the current database loaded on each page load, you'll need to use sessions. In the controller function that handles the dropdown data, you could enter something similar to the following:

$db = $this->input->post('select')

Within any controller that will use the database you'll want to add code to load the current database:

function __construct(){
    $db = $this->session->userdata('db');


If you are going to need to access the same database from various models, I suggest using a library by mddd at EllisLab. Just create a PHP file named Db_manager.php with the folowing code and drop it into your application/libraries directory:

class Db_manager
    var $connections = array();
    var $CI;

    function __construct()
        $this->CI =& get_instance();

    function get_connection($db_name)
        // connection exists? return it
        if (isset($this->connections[$db_name])) 
            return $this->connections[$db_name];
            // create connection. return it.
            $this->connections[$db_name] = $this->CI->load->database($db_name, true);
            return $this->connections[$db_name];

In the constructor of each model that will use the database add the following:

var $current_db;

function __construct(){
    $db = $this->session->userdata('db');
    if ($db == 1){
        $this->current_db = $this->db_manager->get_connection('default');
    } else {
        $this->current_db = $this->db_manager->get_connection('alternate');
Thursday, October 13, 2022
// top of your controller
ini_set('max_execution_time', 0);

// Also you can increase memory

Download this helper and place in system/helpers/

and finally create csv like this

$query = $this->db->get('your_table');
query_to_csv($query, TRUE, 'filename.csv');
Friday, August 12, 2022

From the docs ( ) :

The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file.

So you would do something like this, replacing the values with values from the master database:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";


If you need to maintain a connection to the master database and the customer database, then change the last line to:

$customer_db = $this->load->database($config, TRUE);

// to use the master database:
$this->db->query("SELECT * FROM my_table");

// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");
Friday, September 23, 2022

I need to have multiple databases for different customers. How can I handle multiple databases with Hibernate? Are there any good examples how to do this?

You'll have indeed to create multiple SessionFactory (one per database).

Now I can get hibernate Configuration object when user logs in, but how can I create/get session factory with that object so that there would be only one session factory for one database (of course if multiple databases are used at the same time, then there can be more than one session factory)?

Use some unique Map<SomeKey, SessionFactory>. If a SessionFactory hasn't been created yet, build it and store it in the map.

Thursday, August 25, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :