How do I connect to a SQL Server database in CodeIgniter? How do I connect to a SQL Server database in CodeIgniter? codeigniter codeigniter

How do I connect to a SQL Server database in CodeIgniter?


use use $db['default']['dbdriver'] = 'sqlsrv';
and install Microsoft Drivers for PHP for SQL Server on windows

CodeIgniter MSSQL connection


I couldn't get it to work with the supported driver of mssql so I used sqlsrv

I normally connect with ip,port as the hostname, so I changed that.

pconnect was also giving me issues. I set it to FALSE and it started working.

Here is the configuration I got to work:

$db['default']['hostname'] = '127.0.0.1,1433';$db['default']['username'] = 'username1';$db['default']['password'] = 'secretpassword';$db['default']['database'] = 'databasename';$db['default']['dbdriver'] = 'sqlsrv';$db['default']['pconnect'] = FALSE;$db['default']['db_debug'] = TRUE;$db['default']['cache_on'] = FALSE;$db['default']['autoinit'] = TRUE;$db['default']['stricton'] = FALSE;


Your Configuration code is fine. Further reading here

IF you believe the errors are not showing up, then go to the index.php and on the top place the following snippet to show the errors.

error_reporting(E_ALL);

Other, then this check if the MSSQL service is running and is accessible. May be create a simple .php and try a connection with plain codes.

A non CI file Something like,

<?php$server = 'YOURPC\SQLEXPRESS';$link = mssql_connect($server, 'user', 'pass');if (!$link) {    die('Something went wrong while connecting to MSSQL');}?>