2

I think that can't manage to connect to the database and I don't know why.

I have looked over stackoverflow and found this questions: here and it is not helping me in solving my problem.

I have read the documentation from Codeigniter 3: here and used the option Manually Connecting.

My class from my application controller looks like so:

class home extends CI_Controller {

    /**
     * Class constructor
     * Load database lib
     */
    public function __construct()
    {
            $this->load->database();

    }

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/home.php/welcome
     *  - or -
     *      http://example.com/home.php/welcome/index
     */
    public function index()
    {
        $query = $this->db->get('users');

        foreach ($query->result() as $row)
        {
            var_dump($row->fullName); //testing purpose
        }

        //$this->load->view('home', $data);

    }

The database config from my application looks like so:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'      => '',
    'hostname' => 'localhost',
    'username' => 'user',
    'password' => 'password',
    'database' => 'tasks',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => FALSE
);

And when I access http://localhost/home.php/welcome

I get this error:

Fatal error: Call to a member function database() on null in \www\task\application\controllers\home.php on line 12

I tried var_dump($this->load) and it is a null and from here my assumption that it can't establish a connection to the database.

3 Answers 3

8

Since you are extending the CI_Controller class and have opted to overload the __construct method, you need to just call the parent construct before you can begin taking advantage of CI's core functions.

class home extends CI_Controller
{
    public function __construct()
    {
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it

        $this->load->database();
    }
}

See http://www.codeigniter.com/user_guide/general/controllers.html#class-constructors for further details.

Sign up to request clarification or add additional context in comments.

2 Comments

oO, yes, I have just found now this after your guidance. **Instead of writing database operations right in the controller, queries should be placed in a model, so they can easily be reused later. ** Thank you.
@Starlays You're welcome. Yes, generally queries should be placed into the model when using an MVC architecture.
1

In my case the accepted solution didn't solved the problem.

I solved with:

$CI =& get_instance();//Put this before call $this->

Now instead of calling $this->, use $CI-> instead.

Comments

0

in my case i just added

parent::__construct();

to the constructor

then connect and close like this

$this->db=$this->load->database('DB', TRUE);
//close
$this->db->close();

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.