0

Homemodel.php

    <?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class HomeModel extends CI_Model{

    private $DB1, $DB2;

    function __construct()
    {
        parent::__construct();
        $this->DB1 = $this->load->database('sample');
    }

    public function getData(){

        /* @var $query type */
        $query  = $this->DB1->get('employee');
        return $query->result(); // Standard Query With Multiple Results (Object Version)

    }

}//class 

Home.php(Controller)

    <?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class  Home extends CI_Controller{

//    private $View;

    public function index(){

        $this->load->model('HomeModel');
        $db_data['eduData'] = $this->HomeModel->getData();  

        $this->load->view('View', $db_data);
    }
}

I have tried above way to fetch data from db, but i got error as

Fatal error: Call to a member function get() on a non-object in D:\xampp\htdocs\Codeigniter\application\models\HomeModel.php on line 21

how to fix that error? and also i have another doubt for Controller Home.php file , i have defined index() call default, and i have tried to change that function name i got error how to fix that error also?

Alok Patel
  • 7,842
  • 5
  • 31
  • 47
Balakumar B
  • 770
  • 5
  • 17
  • 41
  • add `TRUE` at `$this->DB1 = $this->load->database('sample',TRUE);` and check!! – Saty Aug 06 '16 at 06:05
  • @Balakumar B have posted my answer should work for you. –  Aug 06 '16 at 06:27
  • After add TRUE i got this error ` An Error Was Encountered You have specified an invalid database connection group (sample) in your config/database.php file. ` – Balakumar B Aug 06 '16 at 06:39
  • @Gordon I don't think duplicate. –  Aug 06 '16 at 07:21
  • @BalakumarB have updated my answer looks like you have not set your database group in database.php –  Aug 06 '16 at 07:24
  • @wolfgang1983 http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12769983#12769983 answers the OP's question, perfectly. – Gordon Aug 06 '16 at 07:32

2 Answers2

0

You need to load the database first. CodeIgniter won't load it by default for you.

You can either add it to /config/autoload.php like so

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

Or you can load it on demand whenever you want by calling

$this->load->database();
Rax Shah
  • 531
  • 5
  • 18
  • You do not need to autoload the library http://www.codeigniter.com/user_guide/database/connecting.html#manually-connecting-to-a-database –  Aug 06 '16 at 06:30
0

You are missing the db in your code.

Change This

$query  = $this->DB1->get('employee');

To This

$query  = $this->DB1->db->get('employee');

Because you have specified sample as a database group you need to make sure it is set up in database.php

Multiple database

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

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'project-1',
    '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' => TRUE
);


$db['sample'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'project-2',
    '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' => TRUE
);

And you have named your model wrong should be named with first letter only upper case.

Model

http://www.codeigniter.com/user_guide/general/models.html#anatomy-of-a-model

Filename Home_model.php

<?php

class Home_model extends CI_Model {

    private $DB1;
    private $DB2;

    public function __construct() {
        parent::__construct();
        $this->DB1 = $this->load->database('sample');
    }

    public function getData() {
        $query  = $this->DB1->db->get('employee');

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }

}

Controller

http://www.codeigniter.com/user_guide/general/controllers.html#let-s-try-it-hello-world

Filename Home.php

<?php

class Home extends CI_Controller {

    public function __construct() {
       parent::__construct();
       $this->load->model('home_model');
    }

    public function index() {

        $db_data['eduData'] = $this->home_model->getData();  

        $this->load->view('View', $db_data);
    }
}
  • i have named my controller `Home` first letter is Uppercase – Balakumar B Aug 06 '16 at 06:41
  • did you try my fix at the top of the Answer `db` –  Aug 06 '16 at 06:42
  • Yes i did but i got error ` An Error Was Encountered You have specified an invalid database connection group (sample) in your config/database.php file. ` after changed ` $query = $this->DB1->db->get('employee');` – Balakumar B Aug 06 '16 at 06:44
  • @BalakumarB I have updated the database.php because sample is a group –  Aug 06 '16 at 06:50