CodeIgniter: Multiple Databases - Accessing database config in a second database - database

I've been looking into using multiple databases with CodeIgniter. If I know what the databases are ahead of time, then I can set the information in the config file and then call whichever database group I need.
In my situation, however, I need to store that database information in another database. It is sort of a master database with general information about a customer including the database and credentials that the customer's data is stored in. This vendor can then add customers whenever they want and have each customer's data segregated in different databases.
How can I set the database and credentials based on the values I get back from the master database in CodeIgniter, or is there even a way to do that?
Can anyone point me in the right direction? Thanks in advance for any advice.

From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html ) :
The first parameter of this function can optionally be used to specify
a particular database group from your config file, or you can even
submit connection values for a database that is not specified in your
config file.
So you would do something like this, replacing the values with values from the master database:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$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);
If you need to maintain a connection to the master database and the customer database, then change the last line to:
$customer_db = $this->load->database($config, TRUE);
// to use the master database:
$this->db->query("SELECT * FROM my_table");
// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");

Make the master a default database and the customer for second database
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['secondDatabase']['hostname'] = '';
$db['secondDatabase']['username'] = '';
$db['secondDatabase']['password'] = '';
$db['secondDatabase']['dbdriver'] = '';
$db['secondDatabase']['dbprefix'] = '';
$db['secondDatabase']['pconnect'] = TRUE;
$db['secondDatabase']['db_debug'] = TRUE;
$db['secondDatabase']['cache_on'] = FALSE;
$db['secondDatabase']['cachedir'] = '';
$db['secondDatabase']['char_set'] = 'utf8';
$db['secondDatabase']['dbcollat'] = 'utf8_general_ci';
$db['secondDatabase']['swap_pre'] = '';
$db['secondDatabase']['autoinit'] = TRUE;
$db['secondDatabase']['stricton'] = FALSE;
you can load the second database in controller or in model by
$DB2 = $this->load->database('secondDatabase', TRUE);

/** config/database.php **/
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = (ENVIRONMENT !== 'production');
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
/** Your controller or model **/
//by default the master database will be loaded and you can directly access db using $this->db
$result = $this->db->query("SELECT * FROM `your_table`")->limit(1)->get()->result();
$config['dbxyz']['hostname'] = $result->hostname;
$config['dbxyz']['username'] = $result->username;
$config['dbxyz']['password'] = $result->password;
$config['dbxyz']['dbdriver'] = '';
$config['dbxyz']['dbprefix'] = '';
$config['dbxyz']['pconnect'] = TRUE;
$config['dbxyz']['db_debug'] = (ENVIRONMENT !== 'production');
$config['dbxyz']['cache_on'] = FALSE;
$config['dbxyz']['cachedir'] = '';
$config['dbxyz']['char_set'] = 'utf8';
$config['dbxyz']['dbcollat'] = 'utf8_general_ci';
$config['dbxyz']['swap_pre'] = '';
$config['dbxyz']['autoinit'] = TRUE;
$config['dbxyz']['stricton'] = FALSE;
//load database config
$this->config->load('database');
//Set database config dynamically
$this->config->set_item('dbxyz', $config);
//Now you can load the new database using
$this->dbxyz = $this->load->database('dbxyz');
NOTE: For more details, refer Config Class Codeigniter documentation

Add below line in application\config\database.php
$db['mydb2']['hostname'] = 'localhost';
$db['mydb2']['username'] = 'root';
$db['mydb2']['password'] = '';
$db['mydb2']['database'] = 'ci2';
$db['mydb2']['dbdriver'] = 'mysql';
$db['mydb2']['dbprefix'] = '';
$db['mydb2']['pconnect'] = TRUE;
$db['mydb2']['db_debug'] = TRUE;
$db['mydb2']['cache_on'] = FALSE;
$db['mydb2']['cachedir'] = '';
$db['mydb2']['char_set'] = 'utf8';
$db['mydb2']['dbcollat'] = 'utf8_general_ci';
$db['mydb2']['swap_pre'] = '';
$db['mydb2']['autoinit'] = TRUE;
$db['mydb2']['stricton'] = FALSE;
Now we use our second database in our Controller and model like below.
$CI = &get_instance();
$this->db2 = $CI->load->database('mydb2', TRUE);
$qry = $this->db2->query("SELECT * FROM employee");
print_r($qry->result());
I have taken reference from http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php .It's work for me.

Related

Unable to connect to your database server using the provided settings in CI

A Database Error Occurred
Unable to connect to your database server using the provided settings.
Filename: /home/rehmantr/public_html/third_party/MX/Loader.php
Line Number: 94
Dear Sir,
I am facing this issue, my database.php file setting is below
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'hostname';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
but still I am facing this issue,please help me.
I am using HMVC pattern of CI
I think its a problem with your credentials, verify that user and password are correct and that the user has access to the database you specify in the settings.

I want to insert data into two different database

I want to insert data into two different database.
Both the database are on different servers & both are different applications.
First application is of Codeigniter.
Second application id of Open Cart.
What should I do to insert the data into both database table.
should I am using nusoap or simple create second data connection and pass the value.
please help me out....
Database configuration:(you can can configure multiple database)
//default/main database
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'database1';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
//another database
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'database2';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = FALSE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;
load the database
$this->db2 = $CI->load->database('db2', TRUE);
insert data using the db instance
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db2->insert('mytable', $data);
more info - http://www.codeigniter.com/userguide2/database/connecting.html
here have an example of connecting two database in ci http://avenir.ro/codeigniter-connect-two-different-databases/

Add database in codeigniter dynamically

I am working on codeigniter app to allow user to add edit and modify database.
I want to repeat this code by loop:
$db['db1']['hostname'] = 'localhost';
$db['db1']['username'] = 'ts4l_wp13';
$db['db1']['password'] = 'O&3D6c(0zD70.^9';
$db['db1']['database'] = 'ts4l_wp13';
$db['db1']['dbdriver'] = 'mysql';
$db['db1']['dbprefix'] = '';
$db['db1']['pconnect'] = FALSE;
$db['db1']['db_debug'] = TRUE;
$db['db1']['cache_on'] = FALSE;
$db['db1']['cachedir'] = '';
$db['db1']['char_set'] = 'utf8';
$db['db1']['dbcollat'] = 'utf8_general_ci';
$db['db1']['swap_pre'] = '';
$db['db1']['autoinit'] = TRUE;
$db['db1']['stricton'] = FALSE;
I do this:
#$get_data = mysql_query("SELECT * FROM `database_name`");
while($getdata = mysql_fetch_assoc($get_data)){
$id='db'.$getdata['id'];
$iid="$id";
$db["$iid"]['hostname'] = 'localhost';
$db["$iid"]['username'] = $getdata['username'];
$db["$iid"]['password'] = $getdata['password'];
$db["$iid"]['database'] = $getdata['name'];
$db["$iid"]['dbdriver'] = 'mysql';
$db["$iid"]['dbprefix'] = '';
$db["$iid"]['pconnect'] = FALSE;
$db["$iid"]['db_debug'] = TRUE;
$db["$iid"]['cache_on'] = FALSE;
$db["$iid"]['cachedir'] = '';
$db["$iid"]['char_set'] = 'utf8';
$db["$iid"]['dbcollat'] = 'utf8_general_ci';
$db["$iid"]['swap_pre'] = '';
$db["$iid"]['autoinit'] = TRUE;
$db["$iid"]['stricton'] = FALSE;
}
and this block.
// Loading db and running query.
$CI = &get_instance();
$this->db1 = $CI->load->database('db1', TRUE);
$this->db1->set($data);
$this->db1->insert($this->_table_name);
$id=$this->db1->insert_id();
I do this:
$this->load->model('mdb_m');
$dbms =$this->mdb_m->get();
if (count($dbms)) {
foreach ($dbms as $dbm) {
$mid=$dbm->id;
$dbc='db'.$mid;
$CI = &get_instance();
$this->db.$mid = $CI->load->database( "$dbc" , TRUE);
$this->db.$mid->set($data);
$this->db.$mid->insert($this->_table_name);
}
}
But it only insert at first database in the array then stop and retrive this error "You have specified an invalid database connection group."
you can get brief information of using multiple database from here.As you have mentioned.
https://ellislab.com/codeigniter/user-guide/database/connecting.html
for better approach you can make a loader file to load a database.I was in same problem and got the clear visualization from here
https://coderwall.com/p/_kyjvg/codeigniter-loading-up-multiple-databases
refer here and if still confused please revert back to me thankyou.

Accessing a database through codeigniter based on login

I have a functionality as follows:
When Any user logs in I want to connect to a particular DB based on login credentials
.
I tried with
$this->db->close();
and this->db->reconnect();
but it did not help
In your application/config/database.php you can define more than one database connection by doing
$db['database1']['hostname'] = 'localhost';
$db['database1']['username'] = 'db1_root';
$db['database1']['password'] = 'xxxxxx';
$db['database1']['database'] = 'database1_name';
$db['database1']['db_debug'] = false; //Important
$db['database2']['hostname'] = 'localhost';
$db['database2']['username'] = 'db2_root';
$db['database2']['password'] = 'xxxxxx';
$db['database2']['database'] = 'database2_name';
$db['database2']['db_debug'] = false; //Important
Then you can load specific databases by doing
$database1 = $this->load->database('database1', true);
$database2 = $this->load->database('database2', true);
Then rather than doing
$this->db->query();
You will need to do either
$database1->query();
$database2->query();
You can connect using a dynamic config in your controller/model/library/whatever by passing a config array to $this->load->database().
$config['hostname'] = "localhost";
$config['username'] = $user_name;
$config['password'] = $user_password;
$config['database'] = $user_database;
$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";
$user_db = $this->load->database($config, true);
I figured out my answer.. Coded as follows:
in the database.php file i have added a new database config:
$db['db2']=$db['default'];
$db['db2']['database'] = 'new_db2';
and then in all the models constructor function i have put the following code:
$new_db = $this->load->database('db2', TRUE );
$this->db = $new_db;
So in this case i don't need to change all my queries. Hurray!!! :)

CodeIgniter 2.1.2 and MS SQL on PHP 5.3.13

I have been at this one all night and our offices open in 69 minutes.
Our server was just updated to PHP 5.3.13 and a critical online application that connects to MS SQL 2008 is just producing a blank page - no errors being logged, just snow. It is written in CodeIgniter 2.1.2.
If I do not autoload ( or try to connect to the db ), the page displays the static elements. Once I add the database.php config file, it's a white-out.
I am trying the mssql and the sqldrv drivers and getting the same results.
I am hoping to find some ideas on how I can go about debugging this solution in an ASAP sort-of-way.
My offending config (which worked until the upgrade) in the database.php looks like this:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "<<SQL SRV NAME>>";
$db['default']['username'] = "<<USERNAME>>";
$db['default']['password'] = "<<PASSWORD>>";
$db['default']['database'] = "<<DATABASE NAME>>";
$db['default']['dbdriver'] = "mssql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
ANY thoughts are GREATLY appreciated.
A bit late now I suspect, but the current version of CodeIgniter (2.1.2) has the following in its Database config file by default:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';
$db['default']['dbdriver'] = 'mssql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
Note the extra couple of configs there at the end. It may be looking for them but as they're not supplied it's erroring...

Resources