0

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?

1
  • Seems that this is a bug of CodeIgniter. See my answer Commented Aug 18, 2012 at 14:02

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.