Welcome Guest, Not a member yet? Register   Sign In
Connecting to second database - model problem.
#1

[eluser]BobbyB[/eluser]
Hi,
I am trying to connect to a second database.
In my controller I have:
Code:
$DB1 = $this->load->database('blog', TRUE);
and my model looks like:
Code:
<?php
class Get_blog extends Model {

  function Get_blog()
    {
        parent::Model();
    }
    

function getselect($limitstart,$limitend)
{
    
    $posts = array();
    $DB1->select('*');
...
I am getting:
Code:
Undefined variable: DB1
It works great if I put the database stuff in the controller.
How do I correctly access the second database from the model?
Thanks in advance!
#2

[eluser]pistolPete[/eluser]
How about that:

Code:
function getselect($limitstart,$limitend)
{  
    $posts = array();
    $DB1 = $this->load->database('blog', TRUE);
    $DB1->select('*');

Or you can pass the database reference to the model:
Code:
// in your controller
function do_something()
{
    $DB1 = $this->load->database('blog', TRUE);
    $this->load->model('get_blog');
    $this->get_blog->getselect(0,1,$DB1);
}

// in your model

function getselect($limitstart, $limitend, $db)
{
   $posts = array();  
   $db->select('*');
...
#3

[eluser]BobbyB[/eluser]
Hey pistolPete,
thanks for your fast reply.
Works like a charm Smile
#4

[eluser]Net_CZ[/eluser]
I do it this way:

Code:
//controller
class Cont extends Controller
{
  public $db2;
  
  public function __construct()
  {
    $this->db2 = $this->load->database('DATABASE', true, true);
  }
}

then you should have access to your second DB everywhere in loaded models etc.




Theme © iAndrew 2016 - Forum software by © MyBB