Welcome Guest, Not a member yet? Register   Sign In
multiple database connections
#1

[eluser]Unknown[/eluser]
hello,

trying to connect to a different database, one with a variable name. no matter what I do, ci keeps the old connection. following the userguide, in my model function I set up

Code:
$config['param'] = 'param';
...
$this->load->database($config);
$this->db->query($sql)

in a method called later,

Code:
$config['newparam'] = 'param';
...
$this->load->database($config);
$this->db->query($sql)

In desperation I have removed my database.php config to try all connections in this fashion, to no avail.
pconnect is false for all, I have tried

Code:
$this->load->database($config,false);

as well as

Code:
$this->different_db = $this->load->database($config);
$this->different_db->query($sql);

was wondering if anyone could lend a hand here, i'm nearly out of ideas.

thanks,
brandon
#2

[eluser]Unknown[/eluser]
also tried closing the connection with

Code:
$this->database->close()

and I would add it to the $db array but the database was just crated. a better code snippet:

controller
Code:
makeuser($u,p$);
create_database($s);
create tables($s);
model
Code:
<?php
class Create_model extends Model{

    function Create_model(){
        parent::model();
    }
    function makeuser($user,$pass){
    $config['hostname'] = 'localhost';
    $config['username'] = 'root';
    $config['password'] = 'password';
    $config['database'] = 'project';
    $config['dbdriver'] = 'mysql';
    $config['dbprefix'] = "";
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    $config['cache_on'] = FALSE;
    $config['cachedir'] = "";
    $config['char_set'] = "utf8";
    $config['dbcollat'] = "utf8_general_ci";
    $this->load->database($config,false);    
    $sql='query';
    $this->db->query($sql);
    $this->db->close();
    }
    function create_database($shortname){
        $config['hostname'] = 'localhost';
        $config['username'] = 'root';
        $config['password'] = 'password';
        $config['database'] = 'project';
        $config['dbdriver'] = 'mysql';
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";
        $this->load->database($config,false);
    $this->load->dbforge();
    $this->dbforge->create_database($shortname);
    $this->db->close();
    }
    function create_tables($shortname){
    $config['hostname'] = 'localhost';
    $config['username'] = 'root';
    $config['password'] = 'password';
    $config['database'] = $shortname;
    $config['dbdriver'] = 'mysql';
    $config['dbprefix'] = "";
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    $config['cache_on'] = FALSE;
    $config['cachedir'] = "";
    $config['char_set'] = "utf8";
    $config['dbcollat'] = "utf8_general_ci";
    $this->load->database($config,false);
      
       //tables created here goto project database?
}
thanks again,
brandon
#3

[eluser]pickupman[/eluser]
Behold the great & mighty user guide. Your question is answered in the last section of the page.

When you use multiple Db's you don't use $this->db but rather $DB1 and $DB2 (or whatever else you want).




Theme © iAndrew 2016 - Forum software by © MyBB