Connect sql database in codeigniter - php

I need to connect two databases in my application. One is MySQL DB and the other one is SQL Server DB. Both hosted in IIS. The connection to MySQL DB is ok, but when I try to connect to SQL DB it shows some error like
Call to undefined function sqlsrv_connect()
The following is my database.php file
$active_group = 'default';
$db['default'] = array(
'dsn' => '',
'hostname' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'database' => 'mydb',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'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['voiceengine'] = array(
'dsn' => '',
'hostname' => 'xxxxx',
'username' => 'xxxxx',
'password' => 'xxxxx',
'database' => 'db1',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
'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 My_model.php is
function checkFile($file) {
$this->db->where('FileName', $file);
return $this->db->count_all_results('voicefile_tbl');
}
function scheduleVoiceSMS($data) {
$voiceengine = $this->load->database('voiceengine', TRUE);
$scheduleData = array();
$this->db->select('Name, Duration');
$this->db->where('FileName', $data['file']);
$result = $this->db->get('voicefile_tbl')->row_array();
if(isset($data['sch_time']) && $data['sch_time'] != ""){
$campaignData = array(
'vaCampaignName' => $result['Name'],
'vaFileName' => $data['file'],
'intFileDurationInSeconds' => $result['Duration'],
'dtScheduledDateTime' => date('Y-m-d H:i:s', strtotime($data['sch_time'])),
'intUserID' => $data['UserID']
);
}
$voiceengine->insert('Campaign', $campaignData);
$id = $this->db->insert_id();
$mobiles = explode(',', $data['destinations']);
foreach($mobiles as $mobile){
if (strlen($mobile) == 12 && substr($mobile, 0, 2) == "91")
$mobile = substr($mobile, 2, 10);
$campaignData = array(
'intCampaignID' => $id,
'intMobileNumber' => $mobile);
array_push($scheduleData, $campaignData);
}
$voiceengine->insert_batch('CampaignNumbers', $scheduleData);
}
I have installed the dll file, and the extension added in php.ini file. But I cannot connect to the database

Related

switch between two databases for different users in codeigniter

I need to change the database name in database.php passing value from the controller. I tried sessions and env variable. but in the database.php cannot access the value from sessions and env variable. also, i need to use the default database to load the page and when user has been logged in need to switch the database. i'm doing this for reducing size of the database data is there any solution really glad someone can help me.
databse.php
$active_group = 'default';
$query_builder = TRUE;
if($db_val==""){
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sliate_srs',
'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
);
}
else{
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => getenv("DB_year"),
'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
);
}
controller.php
function loginSubmit() {
$DB_year=$this->input->post('year_select');
//$this->session->unset_userdata('DB_year');
//$this->session->set_userdata('DB_year',$DB_year);
$DB_year="srs_2019";
putenv("DB_year=$DB_year");
// $_ENV["DB_year"]="srs_2019";
print_r(getenv("DB_year"));
// $DB_year="srs_2019";
$result = $this->Login_model->authenticateLogin();
if (!empty($result)) {
$now = date('Y-m-d H:i:s');
$ip = $this->input->ip_address();
$data = array(
'u_id' => $this->session->userdata('u_id'),
'u_name' => $this->session->userdata('u_name'),
'center_name' => $this->session->userdata('u_branch'),
'last_login_ip' => $this->input->ip_address('ip'),
'last_login_date_time' => $now
);
$name = $this->session->userdata('DB_year');
print_r($name);
$this->Login_model->last_login($data);
redirect('Admin/dashboard');
} else {
redirect('Login?login=invalid');
}
}
Instead of changing the config group in Database.php, why not create a custom connection in your model, as explained in Connecting with Custom Settings?

How to join two tables from different databases using codeigniter?

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

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