我一直在寻找这个问题!但没有什么能真正帮助解决这个问题。
我总是收到以下错误:“致命错误:在非对象上调用成员函数 query()”
我有以下控制器:
<?php
class Station extends CI_Controller {
function __construct($params=array()) {
parent::__construct();
session_start();
$this->load->library('parser');
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->tpl_data['location'] = $this->stationmodel->getLocations()->all;
$_SESSION['location'] = $this->tpl_data['location'];
$this->load->view('station', $this->tpl_data);
}
}
?>
以及以下型号:
<?php
class StationModel extends CI_Model {
function __construct() {
parent::__construct();
}
public function getLocations() {
$query = $this->db->query("SELECT * FROM location WHERE City_IATA = 'MUC'");
var_dump($query);
// Just for test purposes!
foreach($query->result() as $row) {
echo $row->Location_Name_DE . '<br />';
}
}
}
?>
在我的 autoload.php 中,我加载了数据库模块:
$autoload['libraries'] = array('database');
此外,当我尝试直接在控制器中加载数据库模块时,我会得到同样的错误。
我的数据库.php:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '****';
$db['default']['database'] = 'comvel';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
任何人都可以帮助我吗?
提前致谢!