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.

Community
  • 1
  • 1
Starlays
  • 1,039
  • 2
  • 15
  • 29

3 Answers3

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 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 Jun 24 '16 at 16:11
  • 2
    @Starlays You're welcome. Yes, generally queries should be placed into the model when using an MVC architecture. – MonkeyZeus Jun 24 '16 at 16:14
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.

Marcelo Agimóvel
  • 1,668
  • 2
  • 20
  • 25
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();
FaisalAlsalm
  • 109
  • 1
  • 2
  • 12