2016-06-24 2 views
2

데이터베이스에 연결할 수 없으며 그 이유를 모르겠습니다.Codeigniter 3 치명적인 오류 : null에 대한 멤버 함수 database()에 대한 호출

나는 stackoverflow를 살펴본 결과이 질문을 발견 : here 그리고 내 문제를 해결하는데 도움이되지 않습니다.

나는 Codeigniter 3의 설명서를 읽었으며 here을 읽고 수동 연결 옵션을 사용했습니다.

는 내 응용 프로그램 컨트롤러에서

내 클래스는 그래서 다음과 같습니다

$active_group = 'default'; 
$query_builder = TRUE; 

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

을 내가 http://localhost/home.php/welcome

에 액세스 할 때 나는이 얻을 :

class home extends CI_Controller { 

    /** 
    * Class constructor 
    * Load database lib 
    */ 
    public function __construct() 
    { 
      $this->load->database(); 

    } 

    /** 
    * Index Page for this controller. 
    * 
    * Maps to the following URL 
    *  http://example.com/home.php/welcome 
    * - or - 
    *  http://example.com/home.php/welcome/index 
    */ 
    public function index() 
    { 
     $query = $this->db->get('users'); 

     foreach ($query->result() as $row) 
     { 
      var_dump($row->fullName); //testing purpose 
     } 

     //$this->load->view('home', $data); 

    } 

내 응용 프로그램에서 데이터베이스 설정이 너무처럼 보인다 오류 :

Fatal error: Call to a member function database() on null in \www\task\application\controllers\home.php on line 12

var_dump ($ this-> load)를 시도했는데 null이고 여기에서 데이터베이스에 연결할 수 없다는 가정하에 가정합니다.

답변

3

CI_Controller 클래스를 확장하고 __construct 메서드를 오버로드하도록 선택 했으므로 부모 구성 요소를 호출해야 CI의 핵심 기능을 이용할 수 있습니다.

class home extends CI_Controller 
{ 
    public function __construct() 
    { 
     // $this->load does not exist until after you call this 
     parent::__construct(); // Construct CI's core so that you can use it 

     $this->load->database(); 
    } 
} 

자세한 내용은 http://www.codeigniter.com/user_guide/general/controllers.html#class-constructors을 참조하십시오.

+0

oO 예, 방금 안내 ​​한 후에 발견했습니다. ** 컨트롤러에서 바로 데이터베이스 작업을 작성하는 대신 쿼리를 모델에 배치해야 나중에 쉽게 다시 사용할 수 있습니다. ** 고맙습니다. – Starlays

+2

@ 스즈 레이 즈 오신 것을 환영합니다. 예, 일반적으로 쿼리는 MVC 아키텍처를 사용할 때 모델에 배치해야합니다. – MonkeyZeus

0

내 경우에는 허용 된 솔루션으로 문제가 해결되지 않았습니다. 대신, 대신 $ CI->를 사용> $ this- 호출하는 지금

$CI =& get_instance();//Put this before call $this-> 

:

내가 해결할.

관련 문제