Add database in codeigniter dynamically - database

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.

Related

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/

How to Connect to database CI from login

How do I connect to a database using form login, so the username and password will be stored from login form. I don't want to setup a default username and password in database.php; it should be derived from the user login form.
This is my model
function connect(){
$nik=$this->input->post('nik');
$password=$this->input->post('password');
$config['hostname'] = 'localhost';
$config['username'] = $nik;
$config['password'] = $password;
$config['database'] = 'test';
$config['dbdriver'] = 'postgre';
$config['dbprefix'] = '';
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;
$config['port']=5432;
//$_SESSION['conn'] = $db;
$dbo= $this->load->database($config,TRUE);
//redirect('user/index');
//end connecting
This my controller.
function login(){
$this->load->model("databaseconnection");
$this->databaseconnection->connect();
$dbo= $this->load->database($config,TRUE);
if (!$dbo) {
echo "not";
}else{
echo "ok";
}
}
So I want to access database to controller, but still error. very need your helps

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!!! :)

how do I connect codeigniter to a SQL server?

I've seen a lot of posts but none that actually worked. Can someone provide a clean/clear way to connect Codeigniter with a MS SQL Server on a Mac? This is high level but I have yet to see a solution that clearly demonstrates this. Thanks a bunch!
I have already done this, in your application/config/database.php put the following:
// This address is valid if you're on the same domain as the server:
// If not, you can put the ip address or url here as well.
$db['default']['hostname'] = 'DBComputerName\SQLEXPRESS';
// if you need a non default port uncomment and edit
//$db['default']['port'] = '5432';
$db['default']['username'] = 'databaseUsername';
$db['default']['password'] = 'databasePassword';
$db['default']['database'] = 'DatabaseName';
$db['default']['dbdriver'] = 'sqlsrv';
$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';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
I know how frustrating this was, as a word of caution, not all Active Record functions work or work as expected, this is due to many SQL server quirks.
I recommend switching most of your Active Record queries to: $this->db->query();
PS If I remember correctly, MSSQL doesn't have a username preconfigured, you need to create a username and allow db access from outside of the subnet.
Refer to this in order to create the db user:
http://technet.microsoft.com/en-us/library/aa337545.aspx
and allow access:
http://support.webecs.com/KB/a868/how-do-i-configure-sql-server-express-to-allow-remote.aspx
I use the PDO driver... this works for me:
$db['mssql']['hostname'] = 'sqlsrv:server=myserverIP;database=mydb;encrypt=true;trustservercertificate=true';
$db['mssql']['username'] = 'myun';
$db['mssql']['password'] = 'mypw';
$db['mssql']['database'] = 'mydb';
$db['mssql']['dbdriver'] = 'pdo';
$db['mssql']['dbprefix'] = '';
$db['mssql']['pconnect'] = TRUE;
$db['mssql']['db_debug'] = TRUE;
$db['mssql']['cache_on'] = FALSE;
$db['mssql']['cachedir'] = '';
$db['mssql']['char_set'] = 'utf8';
$db['mssql']['dbcollat'] = 'utf8_general_ci';
$db['mssql']['swap_pre'] = '';
$db['mssql']['autoinit'] = TRUE;
$db['mssql']['stricton'] = FALSE;

CodeIgniter: Multiple Databases - Accessing database config in a second 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.

Resources