Welcome Guest, Not a member yet? Register   Sign In
Error when using db library (set to autoload)
#1

[eluser]Ren83[/eluser]
Hi,
when i try this in a model: $this->db->query('some query');

Im getting this error:
Fatal error: Call to a member function query() on a non-object in D:\wamp\www\.........

I DID add 'database ' to the $autoload['libraries'] array on autoload.php.

What is the problem?

Thanks.
#2

[eluser]ghprod[/eluser]
Hi,

where do u load this code? at controller/libraries/models?

dont forget to set $ci = get_instance() if you load in libraries or helper Smile

regards
#3

[eluser]Ren83[/eluser]
I load it in a model (application/models)
#4

[eluser]ghprod[/eluser]
would u mind if you post your model here?

regards
#5

[eluser]Ren83[/eluser]
Code:
<?php

class Staff_m extends CI_Model
{
        private $serial;
        public function __construct() {
                  parent::__construct();
        }

        public function get_staff_data($serial){

                  $this->serial = $serial;
                  $result = $this->db->query('SELECT * FROM staff WHERE serial ='.$serial);

        }
}
?>
#6

[eluser]ghprod[/eluser]
so strange your error occurred only when object "db" wasnt set .. just make sure you already load all required things to run DB object

and dont forget set config/database.php

regards
#7

[eluser]Ren83[/eluser]
I setted config/database.php.

anyone have an idea?
#8

[eluser]Ren83[/eluser]
Here is some more info:
This is the default controller from wich i call to Staff controller -

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function index(){

           $question = new Staff(1);

    }
}

And here is the Staff controller wich call to Staff model the i reminded previous post:

Code:
class Staff extends CI_Controller
{
           private $data;
            public function __construct($serial)
           {
                     parent::__construct();
                     $this->load->model('Staff_m');
                     $this->data = $this->Question_m->get_staff_data($serial);
          }
}




Theme © iAndrew 2016 - Forum software by © MyBB