0

I am creating a new custom library with database connection but am unable to load the database

This is my custom library :

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); 
    }
}

Error

 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

i have defined this library in auto load along with database and session, how can i over come this?

1
  • Change $this->db->select('*'); to $this->CI->db->select('*'); Commented Jul 19, 2018 at 14:58

1 Answer 1

2

Change $this; to $this->CI;

Should be like this :

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); 
}
Sign up to request clarification or add additional context in comments.

4 Comments

Call to a member function result() on null am getting this error @pradeep
getting this Undefined property: CI_DB_mysqli_driver::$get and Fatal error: Call to a member function result() on null in C:\xampp\htdocs\hhcl\application\libraries\Seo.php on line 35 errors
get should be get() see my answer for that
pleasure is mine , happy coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.