Codeigniter error: no database selected

I am using the Codeigniter DBForge class to create a database and a table in this database.

Here is the 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 code is written inside the controller index function. When the following code is executed, it shows an error like, No database selected . Does anyone have an idea of ​​ant why this is happening. Any help is appreciated.

+1
source share
2 answers

You need to specify

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

in database.php

+5
source

Just refer to the database.php file inside the application / config folder. Make sure you have the correct values ​​for the database name and other things in the following lines provided in the database.php file

 $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'your username', //By default it is root 'password' => 'your password', //By default this field is empty 'database' => 'database name',//The error that you are getting is because of this only. Specify your database name here 'dbdriver' => 'mysqli', 

Hope this helps

+1
source

All Articles