title: CodeIgniter & DBForge - Create Database and Tables
body: <p>I'm trying to write a script in CodeIgniter that will create a database and then will add tables to that newly created database along with various fields in to the new table.</p>
  
  <p>So far I've got dbforge to create the new database but since I'm using the database library and have pre-set database connection values, when it goes to create a table, it puts it in the pre-selected database and not the one it just created.</p>
  
  <p>In the database.php config file I have the following:</p>
  
  <pre><code>$db['default']['database'] = 'db1';
  </code></pre>
  
  <p>If I use the following command to create a new database:</p>
  
  <pre><code>$this-&gt;dbforge-&gt;create_database('db2');
  </code></pre>
  
  <p>'db2' will get created but then the following command puts the table in 'db1'.</p>
  
  <pre><code>$this-&gt;dbforge-&gt;create_table('table1');
  </code></pre>
  
  <p>I need 'table1' created in 'db2'. How do I get CI to select the newly created database ('db2') to create the table in the correct place, and then switch back to 'db1'?</p>
  
  <p>I've looked at the following question which is similar to what I'm doing but I do not want to have to put any further connection entries in the database.php</p>
  
  <p><a href="https://stackoverflow.com/questions/16392828/codeigniter-showing-error-no-database-selected">Codeigniter showing error: No database selected</a></p>
  
  <p>Any help appreciated!</p>
  
  <p><strong>EDIT</strong> - I should add that the create_database could have any name for the database name... This is a script to automatically create a database and the relevant tables where the DB name is pulled from a form.</p>