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

时间:2016-08-06 06:00:12

标签: php codeigniter

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

我已经尝试过以上方式从db获取数据,但是我得到了错误

  

致命错误:在非对象中调用成员函数get()   D:\ xampp \ htdocs \ Codeigniter \ application \ models \ HomeModel.php就行了   21

如何修复该错误?而且我对Controller Home.php文件有另一个疑问,我已经定义了index()调用默认值,我试图更改该函数名称我得到错误如何修复该错误?

2 个答案:

答案 0 :(得分:0)

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

您可以将其添加到/config/autoload.php,如此

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

或者您可以随时通过调用

加载它
$this->load->database();

答案 1 :(得分:0)

您错过了代码中的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-of-a-model

文件名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);
    }
}