4

When I try to call

$this->load->database();

yields the following error `"Call to a member function database() on a non-object"
Autoloading the database doesnt help too...
when I try to autoload it.
all calls to database like

$this->db->get('people');

it says get method is undefined...
I have no clue what and where to start..
\ anyone ?

4 Answers 4

12

Go to autoload.php in application/config/autoload.php and add this

$autoload['libraries'] = array('database'); // add database in array

Make sure your connection settings are fine in application/config/database.php

Than in the library do it like this

Class MyLib
{
    function getPeople(){
        $CI =   &get_instance();
        $query  =   $CI->db->get('people');
        return $query->result();
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

:( .. still doesnt work ... I put the code in construct.. and added variable to set the $CI .. still no luck...
if you autoload the database you dont have to do this. simple use $CI->db->get('people'); and it will work
$CI = &get_instance(); .... yikes looks like this code affects other code.....
@KebyangBlabla i have edited my answer and simplified it. Review it to get your problem solved. Also when you have model why use library
I just need to access some contents in my database in my library. &get_instance(); I though it was a typo.. sorry.. Im migrating my code to model.... arghh.... sleepless nights.. woe woe...
|
1

Use extends CI_Model if not working try extends Model

class User_model extends CI_Model { 

     public function __construct() 
     {
           parent::__construct(); 
           $this->load->database();
     }
}

1 Comment

CodeIgnister 4 it's called just Model: extends Model
0

You can load the database by two methods:

Method 1:Automatic Connecting

$autoload['libraries']=array('database');

Method 2: Manual Connecting

$this->load>database();

i hope above methods clear your confusion....

Comments

0

You are doing a very common mistake. When you call $this->load->database(); from controller or model it works because controllers and models are child of CI_Controller and CI_Model respectively. But when you are call them from Library which is not a child class of any basic CIclass you cannot load database() or anything else using $this-> key. you must use the help of &get_instance(); to load codeigniter instance and use that instance instead of $this. Which suggests following Code:

$INST=&get_instance();//Store instance in a variable.
$INST->load->database();//If autoload not used.
$INST->db->get('people');//or Your desired database operation.

It is better to keep a field variable to hold the reference to $INSTas you may need to access it in various functions.

Following Code will be more eligent:

 class MyLib{
     var $INST;
     public function __construct() 
     {
         $INST=&get_instance();//Store instance in a variable.
         $INST->load->database();//If autoload not used.
     }

     function getPeople(){
         $query  =  $INST->db->get('people');
         return $query->result();
     }
 }

Comments

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.