【问题标题】:codeigniter library with database connection具有数据库连接的 codeigniter 库
【发布时间】:2018-07-19 14:56:56
【问题描述】:

我正在创建一个带有数据库连接的新自定义库,但无法加载数据库

这是我的自定义库:

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:\xampp\htdocs\hhcl\application\libraries\Seo.php
    Line: 30
    Function: _error_handler

    File: C:\xampp\htdocs\hhcl\application\controllers\Home.php
    Line: 28
    Function: getMetadata

    File: C:\xampp\htdocs\hhcl\index.php
    Line: 316
    Function: require_once

我已经在自动加载中定义了这个库以及数据库和会话,我该如何克服呢?

【问题讨论】:

  • $this-&gt;db-&gt;select('*'); 更改为$this-&gt;CI-&gt;db-&gt;select('*');

标签: php codeigniter codeigniter-3 codeigniter-query-builder codeigniter-library


【解决方案1】:

$this; 更改为$this-&gt;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); 
}

【讨论】:

  • 在 null 上调用成员函数 result() 我收到此错误 @pradeep
  • 获取此未定义属性:CI_DB_mysqli_driver::$get 和致命错误:调用 C:\xampp\htdocs\hhcl\application\libraries\Seo.php 中的 null 上的成员函数 result()第 35 行错误
  • get 应该是 get() 看看我的答案
  • 快乐是我的,快乐编码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-06
  • 2013-08-28
  • 1970-01-01
相关资源
最近更新 更多