致命错误:在[Codeigniter]中的非对象上调用成员函数get()


Fatal error: Call to a member function get() on a non-object in[Codigniter]

Homemodel.php

    <?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class HomeModel extends CI_Model{
    private $DB1, $DB2;
    function __construct()
    {
        parent::__construct();
        $this->DB1 = $this->load->database('sample');
    }
    public function getData(){
        /* @var $query type */
        $query  = $this->DB1->get('employee');
        return $query->result(); // Standard Query With Multiple Results (Object Version)
    }
}//class 

Home.php(控制器(

    <?php
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class  Home extends CI_Controller{
//    private $View;
    public function index(){
        $this->load->model('HomeModel');
        $db_data['eduData'] = $this->HomeModel->getData();  
        $this->load->view('View', $db_data);
    }
}

我已经尝试了上述方法从数据库中获取数据,但我在中出错了

致命错误:对中的非对象调用成员函数get((D: ''examplep''htdocs''Codeigniter''application''models''HomeModel.php在线21

如何修复那个错误?此外,我对ControllerHome.php文件还有另一个疑问,我已经定义了index((调用默认值,并且我试图更改我遇到错误的函数名。如何修复该错误?

您需要首先加载数据库。CodeIgniter默认情况下不会为您加载它。

你可以像一样将其添加到/config/autoload.php

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

或者,您可以随时调用按需加载

$this->load->database();

您的代码中缺少db

更改此

$query  = $this->DB1->get('employee');

到此

$query  = $this->DB1->db->get('employee');

因为您已将sample指定为数据库组,所以需要确保它已在database.php 中设置

多个数据库

$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'project-1',
    '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' => TRUE
);

$db['sample'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'project-2',
    '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' => TRUE
);

并且您的模型命名错误,应该只使用第一个字母的大写字母命名。

型号

http://www.codeigniter.com/user_guide/general/models.html#anatomy-独一无二的

文件名Home_model.php

<?php
class Home_model extends CI_Model {
    private $DB1;
    private $DB2;
    public function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('sample');
    }
    public function getData() {
        $query  = $this->DB1->db->get('employee');
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
}

控制器

http://www.codeigniter.com/user_guide/general/controllers.html#let-s-try-it-hello-world

文件名Home.php

<?php
class Home extends CI_Controller {
    public function __construct() {
       parent::__construct();
       $this->load->model('home_model');
    }
    public function index() {
        $db_data['eduData'] = $this->home_model->getData();  
        $this->load->view('View', $db_data);
    }
}