CodeIgniter connection to database

27,228

Solution 1

You need to load the database library:

config/autoload.php

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

Solution 2

The problem is that you are not loading database class

$this->load->database();

Here is the Codeigniter User Guide

Without connecting you can not use database class therefore it is saying undefined property db.

Solution 3

For manual connection
Use this in your controller file

$this->load->database();

For automate connection in database

Go to Controller then in autoload.php make the following changes

$autoload['libraries'] = array('database');
Share:
27,228

Related videos on Youtube

Rafał Developer
Author by

Rafał Developer

.NET

Updated on March 20, 2020

Comments

  • Rafał Developer
    Rafał Developer about 4 years

    I decided end my cooperation with Doctrine 2.3 and now I would like to make connection to database using only CodeIgniter have you got idea where is problem with my code?

    This Error I get:

    A PHP Error was encountered
    
    Severity: Notice
    
    Message: Undefined property: Hello::$db
    
    Filename: core/Model.php
    
    Line Number: 51
    

    Database.php

    $db['default']['hostname'] = 'localhost:8080';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '';
    $db['default']['database'] = 'ci_doctrine';
    $db['default']['dbdriver'] = 'mysql';
    

    Controller

    <?php
        // system/application/controllers/hello.php
    
        class Hello extends CI_Controller {
    
            public function __construct()
            {
                 parent::__construct();
            }
           function world() {
                echo "Hello CodeIgniter!";
            }
    
            function user_test() {
    
                $this->load->model('user');
    
                $this->user->save_User('username','password','first_name','last_name');
    
           }
    }
    
    ?>
    

    Model - table name in database "user"

    <?php
        // system/application/models/user.php
    
        class User extends CI_Model {
    
                function __construct()
                {
                    // Call the Model constructor
                    parent::__construct();
                }
    
                 function save_User($username,$password,$fName,$lName)  {
    
                    $this->username   = $username; 
                    $this->password = $password;
                    $this->first_name    = $fName;
                    $this->last_name    = $lName;
                    $this->db->insert('user', $this);
                  }
    
        }
    ?>
    
    • Muhammad Raheel
      Muhammad Raheel about 11 years
      Are you on localhost? if yes then try removing 8080 post
    • Rafał Developer
      Rafał Developer about 11 years
      @raheel shan but if I will remove this port I`m not able to connect to website. My port 80 have Skype and WAMP have 8080.
  • Rafał Developer
    Rafał Developer about 11 years
    you mean this -> $autoload['config'] = database.php; ??
  • Muhammad Raheel
    Muhammad Raheel about 11 years
    yes are you loading database class? you haven't mentioned it in question.
  • mallix
    mallix about 11 years
    No I mean $autoload['libraries'] = array('database'); Folder application/config file autoload.php :)
  • Rafał Developer
    Rafał Developer about 11 years
    I get this -> A Database Error Occurred Unable to connect to your database server using the provided settings. Filename: C:\wamp\www\nauka\system\database\DB_driver.php Line Number: 124
  • mallix
    mallix about 11 years
    Now you have another error on your application/config/database.php file. Change you specs to correct ones, in order to connect to your database.
  • Muhammad Raheel
    Muhammad Raheel about 11 years
    try to free post edit for wamp by going to Skype->tool->settings->advance->connection and uncheck use port 80 and 443 then restart wamp and skype