title: CodeIgniter dynamic database not working
body: <p>I have seen <a href="https://stackoverflow.com/questions/11622872/how-to-connect-with-user-specified-database-in-codeigniter">this topic</a> and tried to implement that way and unfortunatelly isn't working.</p>
  
  <p>I need to dynamically connect to a database with CodeIgniter. This database <strong>cannot be a constant</strong> variable in the file <code>application/config/databases.php</code>. I have tried two different ways of connecting to a different database:</p>
  
  <p>The first one is by model - with third parameter as <a href="http://ellislab.com/codeigniter/user-guide/general/models.html#conn" rel="nofollow noreferrer">documentation refers</a>.</p>
  
  <pre><code>class users extends CI_Controller
  {
     public function __construct()
     {
        parent::_construct();
  
        $config['hostname'] = 'localhost';
        $config['username'] = 'root';
        $config['password'] = '';
        $config['database'] = 'clockin_AlKj';
        $config['dbdriver'] = 'mysqli';
        $config['dbprefix'] = '';
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
  
        $this-&gt;load-&gt;model('company_model', '', $config);
        $this-&gt;company = $this-&gt;company_model-&gt;get();
     }
  }
  </code></pre>
  
  <p>This throws the error:</p>
  
  <blockquote>
    <p>Error Number: 1146</p>
    
    <p>Table 'clockin_admin.company' doesn't exist</p>
    
    <p>SELECT * FROM (<code>company</code>)</p>
    
    <p>Filename: C:\xampp\htdocs\clockin\system\database\DB_driver.php</p>
  </blockquote>
  
  <p>The second way is to connect by using database.</p>
  
  <pre><code> class users extends CI_Controller
   {
      public function __construct()
      {
         parent::_construct();
  
         $this-&gt;load-&gt;model('company_model');
         $this-&gt;company = $this-&gt;company_model-&gt;get();
      }
   }
  
  class Company_model extends CI_model
  {
     private $otherDb;
  
     public function __construct()
     {
        parent::__construct();
  
        $config['hostname'] = 'localhost';
        $config['username'] = 'root';
        $config['password'] = '';
        $config['database'] = 'clockin_AlKj';
        $config['dbdriver'] = 'mysqli';
        $config['dbprefix'] = '';
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
  
        $this-&gt;otherDb = $this-&gt;load-&gt;database($config);
     }
  
     public function get()
     {
        $this-&gt;otherDb-&gt;get('company')-&gt;row();
     }
  }
  </code></pre>
  
  <p>This throws the error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function get() on a non-object in
    C:\xampp\htdocs\clockin\application\models\company_model.php on line
    45</p>
  </blockquote>
  
  <p>Which refers to the line <code>$this-&gt;otherDb-&gt;get('company')-&gt;row();</code></p>
  
  <p>The problem is that if I verify if the connection was established it also returns error.</p>
  
  <pre><code>if($this-&gt;load-&gt;database($config) === FALSE)
     echo 'Yes, I could not connect..';
  </code></pre>
  
  <p>Finally, my configuration of file <code>database.php</code> is the following:</p>
  
  <pre><code>$active_group = 'default';
  $active_record = TRUE;
  
  $db['default']['hostname'] = 'localhost';
  $db['default']['username'] = 'root';
  $db['default']['password'] = '';
  $db['default']['database'] = 'clockin_admin';
  $db['default']['dbdriver'] = 'mysqli';
  $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['default']['swap_pre'] = '';
  $db['default']['autoinit'] = TRUE;
  $db['default']['stricton'] = FALSE;
  </code></pre>