How to use SQL Server 2012 as my database in codeigniter How to use SQL Server 2012 as my database in codeigniter codeigniter codeigniter

How to use SQL Server 2012 as my database in codeigniter


In your application/config/database.php file:

$db['sqlserver']['username'] = 'db_username';$db['sqlserver']['password'] = 'db_password';$db['sqlserver']['database'] = 'db_database';$db['sqlserver']['hostname'] = 'sqlsrv:server=db_servername;Database=' . $db['sqlserver']['database'];$db['sqlserver']['dbdriver'] = 'pdo';$db['sqlserver']['dbprefix'] = '';$db['sqlserver']['pconnect'] = FALSE;$db['sqlserver']['db_debug'] = TRUE;$db['sqlserver']['cache_on'] = FALSE;$db['sqlserver']['cachedir'] = '';$db['sqlserver']['char_set'] = 'utf8';$db['sqlserver']['dbcollat'] = 'utf8_general_ci';$db['sqlserver']['swap_pre'] = '';$db['sqlserver']['autoinit'] = TRUE;$db['sqlserver']['stricton'] = FALSE;

The first four values obviously need to be set to whichever configuration you have (db_username, db_password, db_database, db_servername).

And set the active group:

$active_group = 'sqlserver';

Using PDO is generally more secure (https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059).

Don't forget to install/enable the sqlsrv PDO driver if it hasn't already been done.https://www.microsoft.com/en-us/download/details.aspx?id=20098 - If on Windows


It can be done from setting of databases in config->database.phpadd own configuration of database like

//sql cofig setting

$db['rajeev']['hostname'] = 'localhost';$db['rajeev']['username'] = 'rajeev';$db['rajeev']['password'] = 'rajeev@123';$db['rajeev']['database'] = 'rajeev_ci';$db['rajeev']['dbdriver'] = 'sqlsrv';$db['rajeev']['dbprefix'] = '';$db['rajeev']['pconnect'] = TRUE;$db['rajeev']['db_debug'] = TRUE;$db['rajeev']['cache_on'] = FALSE;$db['rajeev']['cachedir'] = '';$db['rajeev']['char_set'] = 'utf8';$db['rajeev']['dbcollat'] = 'utf8_general_ci';$db['rajeev']['swap_pre'] = '';$db['rajeev']['autoinit'] = TRUE;$db['rajeev']['stricton'] = FALSE;

then set $active_group like this

$active_group = 'rajeev';


@rajeev has one of some solutions, but if it didn't work you can try use ODBC connection. ODBC has function as gateway connection between your CodeIgniter application and SQL Server 2012 database.

change the value of 'sqlsrv' to 'odbc'.

$db['db']['dbdriver'] = 'odbc';

To create ODBC connection you can read this link :http://msdn.microsoft.com/en-us/library/ca6axakh%28v=vs.80%29.aspx