2つのデータベースを使用するcodeigniterを使用してWebサイトを構築しようとしています
しかし、私はそれらを機能させるのに問題があるようです
私が得るエラーは
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;
}
}
model(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();
}
}
config(database.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; ?>
私は何かが足りないのですか?
助けてくれてありがとう