Codeigniter showing error: No database selected

V15HM4Y picture V15HM4Y · May 6, 2013 · Viewed 7.2k times · Source

I am using Codeigniter's DBForge class to create a database and table within that database.

Here is code:

if ($this->dbforge->create_database('new_db')) {
     $fields = array(
         'blog_id' => array(
              'type' => 'INT',
              'constraint' => 5,
              'unsigned' => TRUE,
              'auto_increment' => TRUE
          ),
          'blog_title' => array(
              'type' => 'VARCHAR',
              'constraint' => '100',
          ),
          'blog_author' => array(
              'type' => 'VARCHAR',
              'constraint' => '100',
              'default' => 'King of Town',
          ),
          'blog_description' => array(
              'type' => 'TEXT',
              'null' => TRUE,
          ),
     );
     $this->dbforge->add_field($fields);
     $this->dbforge->add_key('blog_id', TRUE);
     $this->dbforge->create_table('blog', TRUE);
}

The above mentioned code is written inside index function of controller. When the following code is executed, it shows error as, No database selected. Does anyone have ant idea why it is happening. Any help is appreciated.

Answer

Igor S. picture Igor S. · May 6, 2013

You need to specify

 $db['default']['database'] = "new_db"; 

in database.php