How to connect more than two databases in codeigniter - php

I want connect to 3 databases. I don't have any problem if i connect just two databases. When i try to connect 3 databases. i have problem like i cant connect to database two.
this my config at database.php :
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432#',
'database' => 'HSP',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['crm'] = array(
'dsn' => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432#',
'database' => 'CRM',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['pltapol'] = array(
'dsn' => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432#',
'database' => 'pltapol',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
and in my model like this :
private $db2;
private $db3;
public function __construct()
{
parent::__construct();
$this->db2 = $this->load->database('crm', TRUE);
$this->db3 = $this->load->database('pltapol', TRUE);
}
i just can get object from database pltapol, but i dont get object from crm. how to fix it?

I want to answer my question. i tried to use
$this->db2->db_select() or $this->db3->db_select()
if i want to use that connection before $this->db2->query() or before $this->db2->query()
that is work!
happy coding :)

Related

codeigniter how to connect second database based on dynamic values

I want to import data from other database with dynamic connection
in codeigniter user input the database credential in the form and than I want to connect that database and import some data in the primary database. the question is how to set the second database connection with the dynamic values.
this is your first db connection from application/config/database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'first_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
and this is second db connection function from model or controller or helper your choice.
private function second_db(){
return $db['second'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'second_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
}
private function first_db(){
return $db['second'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'first_db',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
}
if you set up your settings then you can switch your database with this code
$this->load->database(first_db(), TRUE);
$this->load->database(second_db(), TRUE);

Unable to Connect SQL server with Codeigniter

I Developed Codeigniter app with SQL server connection. it is properly connected when using PHP 5.6 on windows 7 PC, but same application not working on windows 10 with same PHP version. showing the error
$db['default'] = array(
'dsn' => '',
'hostname' => '192.168.0.241',
'username' => 'sa',
'password' => 'mypw',
'database' => 'dbsrver',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
You might have some error in configuration. Make sure you have correctly configure database.
In application/config/databse.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'write password if any otherwise leave it empty',
'database' => 'Your_Database_Name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

What is the purpose of using $db['read-write'] AND $db['read-write-create'] in Codeigniter?

What is the purpose of using $db['read-write'] AND $db['read-write-create'] in database.php (application/config/database.php) in Codeigniter? Please help me to get an answer .
$db['read-write'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'db_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production') ,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array() ,
'save_queries' => TRUE
);
$db['read-write-create'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => 'root',
'database' => 'db_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production') ,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array() ,
'save_queries' => TRUE
);

Change Database connection array codeigniter

I'm new in CI,
can someone help ?
I have 2 database connections :
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '10.1.0.166',
'username' => 'sa',
'password' => 'Sprite12345',
'database' => 'HRD',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['credit'] = array(
'dsn' => '',
'hostname' => '10.1.0.166',
'username' => 'sa',
'password' => 'Sprite12345',
'database' => 'BHAKTI',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
And now, I'm trying to change the database value of $db['credit'].
something like this :
$db2 = $this->load->database('credit', TRUE);
$db2->database = 'BIT';
echo $db2->database;
$db2 = $this->load->database('credit', TRUE);
$db2->select('*');
$db2->from('tblconfig');
$query = $db2->get()->result();
print_r($query);
But the query result still take the tblconfig from BHAKTI not from BIT.
How can I change the database value in the config/database.php ?
Thanks in advance.

Codeigniter: How can we connect two databases. If so how can we use them simultaneously

I am using this code in CodeIgniter to add a database:
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '12345',
'database' => 'saas',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
How can I add a second database?
And How can i use them simultaneously?
Any help would be greatly appreciated!
From The Documentation:
$db['test'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'database_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'compress' => FALSE,
'encrypt' => FALSE,
'stricton' => FALSE,
'failover' => array()
);
In your model you can write:
function method()
{
$test = $this->load->database('test', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
$query = $test->select('first_name, last_name')->get('person');
var_dump($query);
}
Read More (http://www.codeigniter.com/user_guide/database/configuration.html)

Categories