2015-07-12 102 views
1

我是codeIgnitor的新手,我試圖自己學習它。但是當我跟着本教程 [http://www.codeigniter.com/user_guide/tutorial/news_section.html][1]無法連接到DB codeIgnitor

的特定指令,這是我的數據庫配置

$db['default'] = array(
    'dsn' => '', 
    'hostname' => 'localhost', 
    'username' => 'root', 
    'password' => '', 
    'database' => 'test', 
    'dbdriver' => 'mysqli', 
    'dbprefix' => '', 
    'pconnect' => TRUE, 
    'db_debug' => TRUE, 
    'cache_on' => FALSE, 
    'cachedir' => '', 
    'char_set' => 'utf8', 
    'dbcollat' => 'utf8_general_ci', 
    'swap_pre' => '', 
    'encrypt' => FALSE, 
    'compress' => FALSE, 
    'stricton' => FALSE, 
    'failover' => array(), 
    'save_queries' => TRUE 
); 

我收到以下錯誤:

A PHP Error was encountered 

Severity: Notice 

Message: Undefined property: CI_Loader::$db 

Filename: pages/about.php 

Line Number: 9 

Backtrace: 

File: /Applications/MAMP/htdocs/CodeIgniter/application/views/pages/about.php 
Line: 9 
Function: _error_handler 

File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/Pages.php 
Line: 18 
Function: view 

File: /Applications/MAMP/htdocs/CodeIgniter/index.php 
Line: 292 
Function: require_once 


Fatal error: Call to a member function query() on null in /Applications/MAMP/htdocs/CodeIgniter/application/views/pages/about.php on line 9 
A PHP Error was encountered 

Severity: Error 

Message: Call to a member function query() on null 

Filename: pages/about.php 

Line Number: 9 

Backtrace: 
+1

你是否已經與您在笨配置的憑據創建數據庫? – alariva

+0

你設置了這些參數嗎? '$ active_group ='default'; $ query_builder = TRUE;' –

+0

@Aririva是數據庫已創建 – Keeper01

回答

3

我想那是因爲你沒有不自動加載數據庫庫。

路徑:的application/config/autoload.php

$autoload['libraries'] = array('database'); 

至於爲什麼出Message: Undefined property: CI_Loader::$db

+0

thx @ wolfgang1983這工作 – Keeper01

+0

@ Keeper01我認爲最常見的助手自動加載URL,表格,HTML和庫數據庫,會議等。 – user4419336

0

你可以嘗試使用,建議在控制器裝載的裝載模型在控制器或視圖......

$this->load->model('News_model','News',true);

,然後嘗試插入,更新或刪除使用:

$this->News->insert_function($param1,$param2);

class News_model extends CI_Model { 

    public function insert_function($param1,$param2) 
    { 
      $this->db->query("insert into table ('col1','col2') values ('$param1','$param2')"); 
      //other codes here.... 
    } 
} 

希望這有助於..