goFrendiAsgard
goFrendiAsgard

Reputation: 4084

CodeIgniter dbutil & db point to different database (How to backup current database)

I have a trouble with dbutil and db. Below is my code (a constructor of my model):

public function __construct(){
    // I save my database configuration in CI session
    $this->load->library('session');
    $config = $this->session->userdata("db_config");
    if($config){ // $config is now equal contains of my database configuration
        $this->db = $this->load->database($config, TRUE);
    }else{
        $this->db = $this->load->database();
    }       
    $this->load->dbutil();
    echo '<pre>'.print_r($this->db, TRUE).'</pre>';
    echo '<pre>'.print_r($this->dbutil, TRUE).'</pre>';
}

I found that $this->db and $this->dbutil point to different databases.

$this->db point to database config from session:

....
[username] => root
[password] => toor
[hostname] => localhost
[database] => coba
....

While $this->dbutil point to database config from my configuration file (application/config/database.php):

....
[username] => root
[password] => toor
[hostname] => localhost
[database] => module_devel
....

This is not expected, since I expect both of $this->db and $this->dbutil point to the same database

I've also check PHP: CodeIgniter; Managing two db connections; variable database parameters. But, for me the solution doesn't work at all.

So, anyone can discover what's wrong here?

Upvotes: 0

Views: 1821

Answers (1)

goFrendiAsgard
goFrendiAsgard

Reputation: 4084

It seems that it is a bug of CodeIgniter. But, dbutil is not the only we can use. There are many alternatives to emulate what dbutil can do.

In my case, I want to generate a "create table" script. I end up using

 $query = $this->db->query("SHOW CREATE TABLE `$table_name`");

And to generate SQL insert script, I do manual select, loop through the records and produce the script.

Hope this can help anyone else with similar problem

Upvotes: 0

Related Questions