3

I have a project in which i have to connect with user specified database. I want to implement it in a proper codeigniter's style but i dont know how can i do that codeigniter stores database credentials in a database.php file is there any way to make it dynamic. Or is there any other approach for achieving this? I have googled it but did not find anything helpful. Any help and suggestion would be appreciated.

UPDATE:
The project is about reporting. I have a form in which i got the database login credentials and then generate the report about their database everything would be done on runtime.

1
  • 1
    load the db configs, then modify them then pass them to $this->load-database($config); Commented Jul 24, 2012 at 1:28

3 Answers 3

9

According to the guide, you can manually pass database connectivity settings via the third parameter of $this->load->model:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('Model_name', '', $config);
// or as gorelative notes, to access multiple databases:
$DB2 = $this->load->database($config, TRUE);
Sign up to request clarification or add additional context in comments.

1 Comment

it should go to note you need to assign $this->load->database() to a variable or class object and set the 2nd param to TRUE in order for you to access multiple databases at once. By doing the above, your telling CI to switch from your core db to the clients db. as a result $this->db will be remapped to the clients db's.
4

I know that this is an old topic, but I was looking for this information and believe that other people could need too.

You don’t need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

$this->db->db_select($database2_name)

That solved to me.

Comments

3

First Off A Word of Warning

It should go noted that this probably is not a recommended best idea, unless you limit the users allowed database names or do not allow them to select it themselves.

If the first scenario is a must, please sanitize the data, and if you know its a defined list of db names.. provide them a list and do validation against the list.

That being said:

Here is your described code. I do this in my controller. And reference $db2-> in my model.

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = $customUserDatabase;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$DB2 = $this->load->database($config);

$q = $DB2->where('id', 13)->get('tablename');

if( $q->num_rows() > 0 ){
  return $q->result();
}else{
  return false;
}

http://codeigniter.com/user_guide/database/connecting.html

Reference the part about connecting to multiple databases...


Connecting to Multiple Databases

If you need to connect to more than one database simultaneously you can do so as follows:

$DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);

Note: Change the words "group_one" and "group_two" to the specific group names you are connecting to (or you can pass the connection values as indicated above).

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

When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:

$this->db->query(); $this->db->result(); etc...

You will instead use:

$DB1->query(); $DB1->result(); etc...

Live Working Code Example.

I put a snippet of my Controller/Model using this exact same feature. I had a need to connect to multiple databases using the same credentials/configs across multiple db's.. as a result i was able to use $this->db-> to get configs.

You can see the gist of it here: https://gist.github.com/08a4f45da1ff7e177425

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.