我正在创建一个带有数据库连接的新自定义库,但无法加载数据库
这是我的自定义库:
class Seo {
var $CI;
public function __construct($params = array())
{
$this->CI =& get_instance();
$this->CI->load->helper('url');
$this->CI->config->item('base_url');
$this->CI->load->database();
}
public function getMetadata($pageid){
$this->db->select('*');
$this->db->from('HHCL_PAGES AS A');
$this->db->join('HHCL_META AS B','A.PAGE_ID = B.PAGE_ID','LEFT');
$this->db->join('HHCL_META_OG AS C','A.PAGE_ID = C.PAGE_ID','LEFT');
$this->db->where('A.PAGE_ID',$pageid);
$data = $this->db->get->result();
echo"<pre>";print_r($data);exit;
$this->CI->load->view('home/layouts/header',$data);
}
}
错误
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Seo::$db
Filename: libraries/Seo.php
Line Number: 30
Backtrace:
File: C:xampphtdocshhclapplicationlibrariesSeo.php
Line: 30
Function: _error_handler
File: C:xampphtdocshhclapplicationcontrollersHome.php
Line: 28
Function: getMetadata
File: C:xampphtdocshhclindex.php
Line: 316
Function: require_once
我已经在数据库和会话的自动加载中定义了这个库,我怎么能克服这个问题呢?
将$this;
更改为$this->CI;
应该是这样的:
public function getMetadata($pageid)
{
$this->CI->db->select('*');
$this->CI->db->from('HHCL_PAGES AS A');
$this->CI->db->join('HHCL_META AS B','A.PAGE_ID = B.PAGE_ID','LEFT');
$this->CI->db->join('HHCL_META_OG AS C','A.PAGE_ID = C.PAGE_ID','LEFT');
$this->CI->db->where('A.PAGE_ID',$pageid);
$data = $this->CI->db->get()->result();
echo"<pre>";print_r($data);exit;
$this->CI->load->view('home/layouts/header',$data);
}