我正在尝试使用 codeigniter 构建一个网站,我将在其中使用两个数据库
但是我似乎在使它们工作时遇到了问题
我得到的错误是
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: cms_db
Filename: models/site_model.php
致命错误:在非对象上调用成员函数 select()
这是我的代码
控制器(site.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function __construct()
{
parent::__construct();
// Your own constructor code
$this->load->model("site_model");
$cms_db = $this->load->database('cms', TRUE);
$site_db = $this->load->database('default', TRUE);
}
public function index()
{
$data = $this->_initialize_data();
$this->load->vars($data);
$this->load->view('site/index');
}
public function _initialize_data()
{
$data['cp'] = $this->site_model->get_cp();
$data['op'] = $this->site_model->get_op();
$data['learnfn'] = $this->site_model->get_learnfn();
return $data;
}
}
模型(site_model.php)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// CI 2.0 Compatibility
if(!class_exists('CI_Model')) { class CI_Model extends Model {} }
class Site_model extends CI_Model
{
function get_cp()
{
return $this->db->select()->from("listings")->where("list_UID","2")->get();
}
function get_op()
{
return $this->db->select()->from("listings",0,4)->where("list_UID","1")->get();
}
function get_learnfn()
{
return $cms_db->select()->from("education")->get()->row();
}
}
配置(数据库.php)
//CMS
$active_group = 'cms';
$active_record = TRUE;
$db['cms']['hostname'] = 'localhost';
$db['cms']['username'] = 'root';
$db['cms']['password'] = '';
$db['cms']['database'] = 'db2';
$db['cms']['dbdriver'] = 'mysql';
$db['cms']['dbprefix'] = '';
$db['cms']['pconnect'] = TRUE;
$db['cms']['db_debug'] = TRUE;
$db['cms']['cache_on'] = FALSE;
$db['cms']['cachedir'] = '';
$db['cms']['char_set'] = 'utf8';
$db['cms']['dbcollat'] = 'utf8_general_ci';
$db['cms']['swap_pre'] = '';
$db['cms']['autoinit'] = TRUE;
$db['cms']['stricton'] = FALSE;
//SITE
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db1';
$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;
这就是我回应它们的方式
<?php echo $learnfn->name; ?>
我错过了什么吗?
谢谢您的帮助