I figured out the trick to do this in codeigniter, there are actually three very important things to keep in mind:
- You should have the client installed on your Apache e.g. SQL
SERVER client
- Your "pconnect" should be set to FALSE in config/database.php file
- To use your sqlserver(2nd Connection) you have to use below code in your
constructor or function in which you want to use:
$this->sqlsrvr = $this->load->database('test', true);
Example Database.php File
You have to add multiple entries in your database.php file config folder, i am sharing my test database.php file below:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'jawad';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['test']['hostname'] = "192.168.43.104";
$db['test']['username'] = "sa";
$db['test']['password'] = "password";
$db['test']['database'] = "jawad";
$db['test']['dbdriver'] = "sqlsrv";
$db['test']['dbprefix'] = "";
$db['test']['pconnect'] = FALSE;
PDOallows to connect to any DBMS type.PDOworks and you will guess you can do that.