title: How to load db in codeigniter if database.php changes immediately
body: <pre><code>$result = $this-&gt;masterdb-&gt;query("select max(collegeId)+ 1 as max_id from college");
  
  $row = $result-&gt;result_array();
  $id = $row[0]['max_id'];
  
  $database = "staging_" .date('ymdHis');
  $query_database = "CREATE DATABASE IF NOT EXISTS 
                     $database
                     DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci";
  
  
  /*
   * insert new college database details in config file here
   */
  
  $result = $this-&gt;masterdb-&gt;query($query_database);
  $college_id = $id;
  
  $dats = "\$db['" .$college_id ."'] = array(
      'dsn'   =&gt; '',
      'hostname' =&gt; 'localhost',
      'username' =&gt; 'root',  
      'database' =&gt; '".$database ."',
      'password' =&gt; '*******',
      'dbdriver' =&gt; 'mysqli',
      'dbprefix' =&gt; '',
      'pconnect' =&gt; FALSE,
      'db_debug' =&gt; TRUE,
      'cache_on' =&gt; FALSE,
      'cachedir' =&gt; '',
      'char_set' =&gt; 'utf8',
      'dbcollat' =&gt; 'utf8_general_ci',
      'swap_pre' =&gt; '',
      'encrypt' =&gt; FALSE,
      'compress' =&gt; FALSE,
      'stricton' =&gt; FALSE,
      'failover' =&gt; array(),
      'save_queries' =&gt; TRUE
  );" ;
  $contents = $dats;
  $contents .= PHP_EOL .PHP_EOL;
  file_put_contents(FCPATH.'application/config/database.php', $dats, FILE_APPEND);
  
  $college_database = $this-&gt;load-&gt;database((string)$id, TRUE);
  </code></pre>
  
  <p>When I want to load the database I get an error: </p>
  
  <blockquote>
    <p>You have specified an invalid database connection group (5) in your
    config/database.php file.</p>
  </blockquote>
  
  <p>But again when I hit the browser with the same max id (5). The database is selected. How to select the database immediately after added configuration code at run time. </p>