Welcome Guest, Not a member yet? Register   Sign In
Different Database
#1

[eluser]eiso[/eluser]
I am looking to use/connect to a different database for one of my controllers and one model. What is the least unobtrusive way to do this, I don't want to have to change any of my other models or controllers by using the $DB1 & $DB2 s described in the user guide.

Thank you very much,

Eiso
#2

[eluser]Randy Casburn[/eluser]
Hi eiso,

Welcome to CI!

1) Make sure your second database group is declared with a non-persistent connection type
2) $secondDB = $this->load->database('groupname',TRUE);
3) $secondDB->query('DELETE * FROM USERS');

// JUST kidding with that last statement ;-)

You only need to worry about the naming convention with the second DB. It will keep you out of trouble down the road if you do both. For a retro-fit this will work fine.

Hope this helps,

Randy
#3

[eluser]eiso[/eluser]
I added this in database.php:
Code:
$db['tdb']['hostname'] = "localhost";//localhost

$db['tdb']['username'] = "username";//root

$db['tdb']['password'] = "password";//empty

$db['tdb']['database'] = "databasename";

$db['tdb']['dbdriver'] = "mysql";

$db['tdb']['dbprefix'] = "";

$db['tdb']['pconnect'] = FALSE;

$db['tdb']['db_debug'] = FALSE;

$db['tdb']['cache_on'] = FALSE;

$db['tdb']['cachedir'] = "";

$db['tdb']['char_set'] = "utf8";

$db['tdb']['dbcollat'] = "utf8_general_ci";

This as my model:

Code:
<?php

class Tadmin_model extends Model{
    
    function Tadmin_model(){

        parent::Model();
            
        $tdb = $this->load->database('tdb', TRUE);            
    }
    
    function FInsert($usernames){
        
        $query = $tdb->query("SELECT * FROM following");
        
        return $query->row();
    }    
}

?>

And this is the start of my controller:
Code:
<?php

class Tadmin extends Controller{
    
    function Tradmin(){
        
        parent::Controller();
        
        $this->load->model('tadmin_model');

And I get this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: tdb

Filename: models/tadmin_model.php

Line Number: ...

Fatal error: Call to a member function query() on a non-object in /blablabla/tadmin_model.php on line ...


What am I doing wrong here?

Thank you,

Eiso

p.s. changed some some lines for security purposes
#4

[eluser]Randy Casburn[/eluser]
Your member varible is scoped incorrectly. $this->tdb will provide you the object you seek.

When you only refer to $tdb, you're only in the function's scope. You need to refer to the class' scope with the $this keyword.

Randy
#5

[eluser]eiso[/eluser]
Thank you Randy - I changed it and am now getting the following error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Tadmin_model::$tdb

Filename: models/tadmin_model.php

Line Number: ...
#6

[eluser]Randy Casburn[/eluser]
Right...your assignment statement statement must include $this as well:

Code:
<?php

class Tadmin_model extends Model{
    
   var $tdb;

   function Tadmin_model(){

        parent::Model();
            
        $this->tdb = $this->load->database('tdb', TRUE);            
    }
    
    function FInsert($usernames){
        
        $query = $this->tdb->query("SELECT * FROM following");
        
        return $query->row();
    }    
}

Let's see if that fixes things up.

Randy
#7

[eluser]eiso[/eluser]
It does! Thank you so much Randy, your help really has been greatly appreciated.

Thank you!!

Eiso
#8

[eluser]Randy Casburn[/eluser]
Your very welcome. Glad to help. Have fun with CI!

Randy




Theme © iAndrew 2016 - Forum software by © MyBB