title: Codeigniter dynamic database connection
body: <p>I am a beginner at php/OOP and have a question about dynamically changing my database connection.</p>
  
  <p>Here is what my main.php looks like:</p>
  
  <pre><code>&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Main extends CI_Controller {
  
  
  function __construct()
  {
  
      parent::__construct();
  
      /* Standard Libraries of codeigniter are required */
      $this-&gt;load-&gt;database($connectdb);
      $this-&gt;load-&gt;helper('url');
      $this-&gt;load-&gt;library('grocery_CRUD');
  }
  
  public function index()
  {
  
  
              if ($_POST["username"] == "root")
                      {
                              $connectdb="default";
                      }
  
  
                      if ($_POST["username"] == "user1")
                      {
                              $connectdb="default1";
                      }
  
              if ($_POST["username"] == "user2")
                      {
                              $connectdb="default2";
                      }
  
  
      $connect = @mysql_connect("localhost", $_POST["username"], $_POST["password"]);//won't display the warning if any.
      if (!$connect)
      {
              echo 'Server error. Please try again sometime. CON';
      }else{
              print("&lt;a href=\"http://v-admindb/ci/index.php/main/employees?username=".$_POST["username"]."\"&gt;Employees&lt;/a&gt;");
              echo "&lt;br&gt;";
              print("&lt;a href=\"http://v-admindb/ci/index.php/main/visitors?username=".$_POST["username"]."\"&gt;Visitors&lt;/a&gt;");
      }//Just an example to ensure that we get into the function
  // LOAD LIBRARIES
  }
  
  public function employees()
  {
      $this-&gt;grocery_crud-&gt;set_table('employees');
      $output = $this-&gt;grocery_crud-&gt;render();
      $this-&gt;_example_output($output);
  }
  
  public function visitors()
  {
      $this-&gt;grocery_crud-&gt;set_table('visitors');
      $output = $this-&gt;grocery_crud-&gt;render();
      $this-&gt;_example_output($output);
  }
  
  
  function _example_output($output = null)
  
  {
      $this-&gt;load-&gt;view('our_template.php',$output);
  }
  }
  </code></pre>
  
  <p>Here is my <code>database.php</code>:</p>
  
  <pre><code>$db['default']['hostname'] = 'localhost';
  $db['default']['username'] = 'root';
  $db['default']['password'] = 'root';
  $db['default']['database'] = 'my_new_cms';
  $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;
  
  
  //CONNECTION FOR user1
  $db['default1']['hostname'] = 'localhost';
  $db['default1']['username'] = 'user1';
  $db['default1']['password'] = 'user1';
  $db['default1']['database'] = 'my_new_cms';
  $db['default1']['dbdriver'] = 'mysql';
  $db['default1']['dbprefix'] = '';
  $db['default1']['pconnect'] = TRUE;
  $db['default1']['db_debug'] = TRUE;
  $db['default1']['cache_on'] = FALSE;
  $db['default1']['cachedir'] = '';
  $db['default1']['char_set'] = 'utf8';
  $db['default1']['dbcollat'] = 'utf8_general_ci';
  $db['default1']['swap_pre'] = '';
  $db['default1']['autoinit'] = TRUE;
  $db['default1']['stricton'] = FALSE;
  
  
  //CONNECTION FOR user2
  $db['default2']['hostname'] = 'localhost';
  $db['default2']['username'] = 'user2';
  $db['default2']['password'] = 'user2';
  $db['default2']['database'] = 'my_new_cms';
  $db['default2']['dbdriver'] = 'mysql';
  $db['default2']['dbprefix'] = '';
  $db['default2']['pconnect'] = TRUE;
  $db['default2']['db_debug'] = TRUE;
  $db['default2']['cache_on'] = FALSE;
  $db['default2']['cachedir'] = '';
  $db['default2']['char_set'] = 'utf8';
  $db['default2']['dbcollat'] = 'utf8_general_ci';
  $db['default2']['swap_pre'] = '';
  $db['default2']['autoinit'] = TRUE;
  $db['default2']['stricton'] = FALSE;
  </code></pre>
  
  <p>When I try logging in I can this error:</p>
  
  <blockquote>
    <p>A PHP Error was encountered</p>
    
    <p>Severity: Notice</p>
    
    <p>Message: Undefined variable: connectdb</p>
    
    <p>Filename: controllers/main.php</p>
    
    <p>Line Number: 12 An Error Was Encountered</p>
    
    <p>You have not selected a database type to connect to.</p>
  </blockquote>
  
  <p>Changing <code>$this-&gt;load-&gt;database($connectdb)</code> to <code>default</code>, <code>default1</code>, or <code>default2</code> makes it work.  How do I put a variable in there so that the connect parameters change depending on who is logging in?</p>
  
  <p>Hopefully someone can help, thanks!
  Eric</p>