4

I have some (model)methods and they connect separately to different databases.

I created two database configs from database.php, loaded them both in the model and created two methods; one connects to DB1, and other to DB2. (sample codes below)

When I replace $this->db by $DB1 or $DB2, I'm getting errors like:

Message: Undefined variable: DB1 // or DB2

Else, I get this error:

Message: Undefined property: Home::$db

I tried to include $DB= $this->load->database("database_name", TRUE); in each method to connect to a specific database. It works but I know its not a good practice connecting it again and again as I reuse the method.

I'm really confused.

Below are my codes:

database.php

$db['db1'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'db1',
    '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['db2'] = array(
    'dsn'   => '',
    'hostname' => 'other_host',
    'username' => 'root',
    'password' => '',
    'database' => 'db2',
    'dbdriver' => 'mssql',
    '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
);

Model:

function __construct()
{
    parent::__construct();
    $DB1= $this->load->database("db1", TRUE);
    $DB2= $this->load->database("db2", TRUE);
}

public function get_from_db1($id)
{
    // $query = $this->db->get_where("my_table1",array("ID"=>$id));
    $query = $DB1->get_where("my_table1",array("ID"=>$id));
    return $query->row_array();
}


public function get_from_db2($id)
{
    // $query = $this->db->get_where("my_table2",array("ID"=>$id));
    $query = $DB2->get_where("my_table2",array("ID"=>$id));
    return $query->row_array();
}

Also:

  • Do I declare and load my databases right?
  • Where should I put the $this->load->database(); function, in the Controller or Model?

Please help!

Thanks in advance guys!

2 Answers 2

5

You need to define variable outside you constructor

public $DB1;
public $DB2;

And call it inside constructor as

function __construct()
{
parent::__construct();
$this->DB1= $this->load->database("db1", TRUE);
$this->DB2= $this->load->database("db2", TRUE);
}

And inside your function

public function get_from_db1($id)
{
// $query = $this->db->get_where("my_table1",array("ID"=>$id));
$query = $this->DB1->get_where("my_table1", array("ID" => $id));
return $query->row_array();
}


public function get_from_db2($id)
{
// $query = $this->db->get_where("my_table2",array("ID"=>$id));
$query = $this->DB2->get_where("my_table2", array("ID" => $id));
return $query->row_array();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Sir, is it really all DB1 on variable declaration and on methods? or just a typo?
It's a typo!! Corrected .
1

did you load the main database class in the autoload.php:

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

you need this library to be loaded so that you can use the $db property.

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.