PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] Codeigniter 3 Fatal error: Call to a member function database() on null

 March 12, 2022     codeigniter, php     No comments   

Issue

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.


Solution

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.



Answered By - MonkeyZeus
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing