title: Codeigniter using sessions in Database class
body: <p>I need your help in codeigniter project. I want to use session to dynamic change data base on databse file in config folder. 
  I do $db['default']['database'] = $_SESSION['db_name'], in config file but dont work.
  The login page Should check which other database access to do login.</p>
  
  <p>Is it possible to change the connection before selecting the database?</p>
  
  <p>Login page</p>
  
  <pre><code>public function login(){
  if(!$this-&gt;session-&gt;userdata('id_funcionario') || !$this-&gt;session-&gt;userdata('logado')){ 
  
      $this-&gt;db-&gt;database= "test";
      $this-&gt;session-&gt;destroy();
      $subdomain = $_SERVER['HTTP_HOST'];  
      $this-&gt;db-&gt;select("database");
      $this-&gt;db-&gt;where("subdomain", $subdomain);
      $access = $this-&gt;db-&gt;get("table")-&gt;row();
      $this-&gt;session-&gt;set_userdata("database", $access-&gt;database);
      $this-&gt;load-&gt;view('/geral/login'); 
  
  }else{
      $url = base_url('home');
      header("Location: $url ");
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The login page works!</p>
  
  <p>The data are send for AJAX to:</p>
  
  <pre><code>&lt;?php if(!defined('BASEPATH')) exit('No direct script access allowed');
  class Ajax extends CI_Controller {
  
      public function __construct(){
          parent::__construct();
          $this-&gt;load-&gt;helper('url');
          $this-&gt;load-&gt;helper('html');
          $this-&gt;load-&gt;library('session');
  
      }
  public function toLog()
      { 
          $email = $this-&gt;input-&gt;post('email');
          $pass= md5($this-&gt;input-&gt;post('pass'));
  
          $this-&gt;db-&gt;database = $this-&gt;session-&gt;userdata('database');
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('users');
          $this-&gt;db-&gt;where('usu_email',$email);
          $this-&gt;db-&gt;where('usu_pass',$pass);
          $usuario = $this-&gt;db-&gt;get()-&gt;result();
  </code></pre>
  
  <p>But I do not know how to change the name of the database on time of the query. I have tried and it remains unchanged.</p>
  
  <pre><code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  
  /**
   * Database Driver Class
   *
   * This is the platform-independent base DB implementation class.
   * This class will not be called directly. Rather, the adapter
   * class for the specific database will extend and instantiate it.
   *
   */
  
      class CI_DB_driver {
  
      var $username;
      var $password;
      var $hostname;
      var $database;
  
  .......
  
      /**
       * Constructor.  Accepts one parameter containing the database
       * connection settings.
       *
       * @param array
       */
      function __construct($params)
      {
          if (is_array($params))
          {
              foreach ($params as $key =&gt; $val)
              {
                  $this-&gt;$key = $val;
              }
          }
          $CI = &amp; get_instance();
          $this-&gt;database = $CI-&gt;session-&gt;userdata('database');
  
          log_message('debug', 'Database Driver Class Initialized');
      }....
  </code></pre>
  
  <p>Could you help me? </p>