How to join two tables from different databases using codeigniter? - php

How to make this query join two tables from different databases?. I already have configured database.php in CodeIgniter with the two databases.
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '******',
'password' => '******',
'database' => '******',
'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['simak'] = array(
'dsn' => '',
'hostname' => '103.124.44.13',
'username' => '******',
'password' => '******',
'database' => '******',
'dbdriver' => 'mysqli',
'port' => 21,
'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
);
My model
function __construct() {
$this->db_simak = $this->load->database('simak', true);
}
private function List() {
$this->db->select('a.*, b.nama_diklat, c.kode_ese');
$this->db->from('kis_diklats a');
$this->db_simak->join('md_diklat b', 'a.id_diklat = b.id_diklat', 'left');
$this->db_simak->join('m_unor c', 'a.kode_ese = c.kode_ese', 'left');
if ($this->session->userdata('level') != 1) {
$this->db->where('a.dihapus_oleh', NULL);
}
Error notice
A Database Error Occurred
Error Number: 1054
Unknown column 'b.nama_diklat' in 'field list'
SELECT a.*, b.nama_diklat, c.kode_ese FROM kis_diklats a ORDER BY diinput_tgl DESC LIMIT 10

Related

Join Query from 2 Table in 2 Different Databases in CodeIgniter

I have 2 databases needs to be joined.
Here is the query that I have but the $db2 is only for the 2nd database, and $this->db->query only for 1st database.
$akses = $db2->query("select A.uName, A.userPwd, A.[Nomor Induk], B.id_jabatan, B.nama FROM tUser A
LEFT JOIN karyawan B ON B.nik = A.[Nomor Induk]
LEFT JOIN bagian_departemen C ON C.id_bagian_dept = B.id_bagian_dept
WHERE A.uName = '$username' AND A.userPwd = '$password'"
);
I want to know the syntax to join 2 table from 2 databases.
Databases:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'helpdesk',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'striction' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['another_db'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'produksi',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'striction' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
try like this:
$this->db->select("db1.table.column")
$this->db->join('db1.table', 'db1.table.column = db2.table.column');
$query = $this->db->get();

How to add two database connections(localhost,web server) in Codeigniter Database Config?

I just want to know how to make two active connections in CG(Database Config) one is Localhost the other one is web. I want to have two database connections for fail safe purpose.I have not try to code it yet but an Idea or a sample code will be a great help. (how to add webserver connection?)
here's the single connection(local):
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array('dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'projectbox_db',
'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);
if(ENVIRONMENT == 'production') {
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array('dsn' => '',
'hostname' => 'localhost',
'username' => 'server_username',
'password' => 'server_password',
'database' => 'projectbox_db',
'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);
} else {
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array('dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'projectbox_db',
'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);
}
You have to change the environment, according to the local code and your production code

Queries on multiple database with codeIgniter

I have 2 databases, and I would like to make a query with the 2 databases, like for example
SELECT base1.table1.item1 FROM base1.table1 INNER JOIN base2.table3 ON base2.table3.item2 = base1.table1.item2 WHERE base2.table3.item4 = 'toto';
How to make this query with codeIgniter ?
I already have configured database.php in CodeIgniter with the 2 databases.
Thanks.
You can setup 2 database in config/database.php file
$active_group = 'default';
$query_builder = TRUE;
$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
);
//set second db configuration
$db['otherdb'] = 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
);
When you want use default database means master database
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$data = $otherdb->get('table_name');
if your first db name is base1 and second is base2
$this->db->select('table1.item1 FROM table1');
$this->db->from('table1');
$this->db->join('base2.table3', 'base2.table3.item2 =table1.item2');
$this->where('base2.table3.item4','toto')
$query = $this->db->get();

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