title: Loading Multiple Databases in CodeIgniter
body: <p>im trying to build a website using codeigniter where i will be using two databases</p>
  
  <p>however i seem to be having a problem in making them work</p>
  
  <p>the error that i get is </p>
  
  <pre><code>A PHP Error was encountered
  
  Severity: Notice
  
  Message: Undefined variable: cms_db
  
  Filename: models/site_model.php
  </code></pre>
  
  <p>Fatal error: Call to a member function select() on a non-object</p>
  
  <p>here is my code</p>
  
  <p><strong>controller(site.php)</strong></p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Site extends CI_controller {
  
  public function __construct()
  {
      parent::__construct();
      // Your own constructor code
      $this-&gt;load-&gt;model("site_model");
  
      $cms_db = $this-&gt;load-&gt;database('cms', TRUE);
      $site_db = $this-&gt;load-&gt;database('default', TRUE);
  
  } 
  
  public function index()
  {
  $data = $this-&gt;_initialize_data();
  $this-&gt;load-&gt;vars($data);
  $this-&gt;load-&gt;view('site/index');
  }
  
  public function _initialize_data()
  {
      $data['cp'] = $this-&gt;site_model-&gt;get_cp();
      $data['op'] = $this-&gt;site_model-&gt;get_op();
      $data['learnfn'] = $this-&gt;site_model-&gt;get_learnfn();
      return $data;
  }
  
  
  }
  </code></pre>
  
  <p><strong>model(site_model.php)</strong></p>
  
  <pre><code>    &lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
      //  CI 2.0 Compatibility
      if(!class_exists('CI_Model')) { class CI_Model extends Model {} }
  
      class Site_model extends CI_Model
      {   
          function get_cp()
          {
              return $this-&gt;db-&gt;select()-&gt;from("listings")-&gt;where("list_UID","2")-&gt;get();
          }
  
          function get_op()
          {
              return $this-&gt;db-&gt;select()-&gt;from("listings",0,4)-&gt;where("list_UID","1")-&gt;get();
          }
  
          function get_learnfn()
          {
              return $cms_db-&gt;select()-&gt;from("education")-&gt;get()-&gt;row();
          }
      }
  </code></pre>
  
  <p><strong>config(database.php)</strong></p>
  
  <pre><code>    //CMS
      $active_group = 'cms';
      $active_record = TRUE;
  
      $db['cms']['hostname'] = 'localhost';
      $db['cms']['username'] = 'root';
      $db['cms']['password'] = '';
      $db['cms']['database'] = 'db2';
      $db['cms']['dbdriver'] = 'mysql';
      $db['cms']['dbprefix'] = '';
      $db['cms']['pconnect'] = TRUE;
      $db['cms']['db_debug'] = TRUE;
      $db['cms']['cache_on'] = FALSE;
      $db['cms']['cachedir'] = '';
      $db['cms']['char_set'] = 'utf8';
      $db['cms']['dbcollat'] = 'utf8_general_ci';
      $db['cms']['swap_pre'] = '';
      $db['cms']['autoinit'] = TRUE;
      $db['cms']['stricton'] = FALSE;
  
      //SITE
      $active_group = 'default';
      $active_record = TRUE;
  
      $db['default']['hostname'] = 'localhost';
      $db['default']['username'] = 'root';
      $db['default']['password'] = '';
      $db['default']['database'] = 'db1';
      $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['default']['swap_pre'] = '';
      $db['default']['autoinit'] = TRUE;
      $db['default']['stricton'] = FALSE;
  </code></pre>
  
  <p>and this is how i echo them out</p>
  
  <pre><code>&lt;?php echo $learnfn-&gt;name; ?&gt;
  </code></pre>
  
  <p>am i missing something?</p>
  
  <p>thanks for the help     </p>