Welcome Guest, Not a member yet? Register   Sign In
Change database in controller
#1

[eluser]huzzel[/eluser]
Hi,

i tried to add a dynamic database via dsn or $db but it wont connect :-(

Code:
function view($id){
        // get mysql details
        $data['site'] = $this->databaseModel->get_by_id($id)->row();

        $db['current']['hostname'] = $data['site']->mysql_ip;
        $db['current']['username'] = $data['site']->mysql_user;
        $db['current']['password'] = $data['site']->mysql_pw;
        $db['current']['database'] = $data['site']->mysql_db;

        $dsn = 'dbdriver://username:password@XXXX/database?char_set=utf8&dbcollat=utf8_general_ci&cache_on=true&cachedir;=/path/to/cache';

        $db_current = $this->load->database($dsn);

        print_r($db_current);

        // load view
        $this->load->view('typolightView', $data);
    }
#2

[eluser]pistolPete[/eluser]
Did you really just copy that line from the user guide and expected it to work?
Code:
$dsn = 'dbdriver://username:password@XXXX/database...';

You need to build the $dsn string using your config settings:
Code:
$dsn = 'mysql://'.$data['site']->mysql_user.':'.$data['site']->mysql_pw.'@'.$data['site']->mysql_ip.'/'.$data['site']->mysql_db.';
#3

[eluser]huzzel[/eluser]
yes and $db_current is still empty :-(

Code:
<?php
class Typolight extends Controller {
    // num of records per page
    private $limit = 10;

    function Typolight(){
        parent::Controller();

        // load library
        $this->load->library(array('table','validation'));

        // load helper
        $this->load->helper('url');

        // load model
        $this->load->model('databaseModel','',TRUE);
    }
    
    function view($id){
        // set common properties
        $data['title'] = 'Typolight Content';
        $data['id'] = $id;
        $data['link_back'] = anchor('backsite/index/','Back to list of backsites',array('class'=>'back'));

        // get backsite details
        $data['backsite'] = $this->databaseModel->get_by_id($id)->row();
        
        $db['current']['hostname'] = $data['backsite']->mysql_ip;
        $db['current']['username'] = $data['backsite']->mysql_user;
        $db['current']['password'] = $data['backsite']->mysql_pw;
        $db['current']['database'] = $data['backsite']->mysql_db;

        $dsn = 'mysql://'.$data['backsite']->mysql_user.':'.$data['backsite']->mysql_pw.'@'.$data['backsite']->mysql_ip.'/'.$data['backsite']->mysql_db;

        $db_current = $this->load->database($dsn);
        
        print_r($db_current);
        
        // load view        
        $this->load->view('typolightView', $data);
    }
}
#4

[eluser]pistolPete[/eluser]
Read the user guide: http://ellislab.com/codeigniter/user-gui...cting.html

Quote:Connecting to Multiple Databases
...

By setting the second parameter to TRUE (boolean) the function will return the database object.

...
#5

[eluser]huzzel[/eluser]
oh ^_^

thx for your help :-)
i am really new to ci. I think it would be better if someone could create a new category here with "newbie questions" ^^




Theme © iAndrew 2016 - Forum software by © MyBB