2013-05-06 70 views
1

我正在使用Codeigniter的DBForge類在該數據庫內創建數據庫和表。Codeigniter顯示錯誤:未選擇數據庫

這裏是代碼:

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); 
} 

上面提到的代碼是控制器的指數函數內寫入。執行以下代碼時,顯示錯誤爲:未選擇數據庫。有沒有人有螞蟻的想法爲什麼發生。任何幫助表示讚賞。

回答

5

您需要在database.php中

1

指定

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

只是參考裏面的應用程序/ config文件夾爲database.php文件。請確保你有一個數據庫名稱和存在於database.php中文件中以下行

$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', 

希望正確的價值觀其他的東西可以幫助

相關問題