Welcome Guest, Not a member yet? Register   Sign In
How to Switch Databases in CI
#1

[eluser]Unknown[/eluser]


i'm new to CI and i just wanna know is there a way to switch databases in CI,Eg: In login Page, there's a dropdown list that you can specify which database you wanna connect with, view look like this
Code:
[b]<select name="select" id="select"  >
  <option value="1">DB1</option>                
  <option value="2">DB2</option>              
</select>[/b]

And i got 2 databases in database.php one is defaultthe other is local by default i'm using the default one.

Code:
$active_group = 'default'; $active_record = TRUE;

My question is how do i specify db based on the dropdown value, say if i pick DB1, connect to default one, DB2 goes to local one.... I know how to swtich db manually by :

Code:
$this->load->database('default', TRUE); OR
$this->load->database('local', TRUE);

Since i got different controller and model, how can i achieve this... i did try like: first get the value of dropdownlist in login function:

Code:
$this->load->model('My_Model');
$db = $this->input->post('select')
$this->My_Model->getDB($db);

and then in my model i got function :

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

Unfortunately, it doesn't work..... Any help would be much Appreciated!!!!
#2

[eluser]pickupman[/eluser]
If you are not using the database simultaneously, you can have a local database, and the live database. If this is the case, just create a subfolder (development) inside your /application/config folder, so you have application/config/development. Any configuration file you put into the development will be used rather than the default.

It works by setting the environment variable at the top of index.php. Open that file, and change on your local copy.
#3

[eluser]XMadMax[/eluser]
I have the same source code for all environments, this allows to be more secure to upload code to prod servers.

In index.php:

Code:
switch ($_SERVER['HTTP_HOST'])
{
case 'www.myserver.com':
  define('ENVIRONMENT', 'production');
break;
case 'www.myserver.local':
  define('ENVIRONMENT', 'development');
break;
case 'www.myserver.myintegrationserver.com':
  define('ENVIRONMENT', 'integration');
break;
}

In your config directory, create this folders:
Code:
development
production
integration

In your config/database.php file, add this line at the end:
Code:
include(strtolower(ENVIRONMENT).'/env_database.php');

The file inside development folder MUST to be different name. If you have ENVIRONMENT defined, Codeigniter tries to load it instead of the base one. The method explained allow you to extend config, firts is loaded the base condig, and overloaded by ypur specific one.

Inside env_database.php:
Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'root';
$db['default']['database'] = 'mylocaldatabase';
$db['default']['dbdriver'] = 'mysqli';

All $db default values will be loaded from /config/database.php, in your env_database.php file, you only put the values overloaded.




Theme © iAndrew 2016 - Forum software by © MyBB