Codeigniter showing error: No database selected

我只是一个虾纸丫 提交于 2020-01-22 00:20:57

问题


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.


回答1:


You need to specify

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

in database.php




回答2:


Just refer to database.php file inside application/config folder. Make sure you have proper values for database name and other things in the following lines present in 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 it helps




回答3:


If you have set the default database in config/database.php and it is still not working, you probably should save the file in case you forget to do that.



来源:https://stackoverflow.com/questions/16392828/codeigniter-showing-error-no-database-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!