How to connect Codeigniter with MSSQL (SQL Server)?

30,855

Solution 1

it will be helpful for someone

Detail steps for How to connect SQL Server with PHP in WampServer

step 1)

download the appropriate driver based on your php version, check using php_info() function, for me 5.6, so the appropriate driver is SQLSRV30, found in the following link https://www.microsoft.com/en-us/download/details.aspx?id=20098

step 2) extract the drives in C:\wamp\bin\php\php5.6.19\ext focus on the following .dll file they should be there, otherwise we can't connect with SQL, these are

php_sqlsrv_56_nts.dll and php_sqlsrv_56_ts.dll

step 3) enable the drives in php.ini as follow, which is found in C:\wamp\bin\apache\apache2.4.18\bin as follow

extension=php_sqlsrv_56_ts.dll extension=php_sqlsrv_56_nts.dll

step 4) go to DB_driver.php line 96 found in C:\wamp\www\public\system\database\DB_driver.php please replace the mysqli by sqlsrv

public $dbdriver = ' mysqli  '; 
 public $dbdriver = 'sqlsrv'; 

step 5) last and most important part, go to CI database config in database.php file which's found in C:\wamp\www\public\application\config\database.php adjust the params accordingly...

$db['default'] = array(

'dsn' => '',
'hostname' => 'ip address for sql server,port', // it should be SQL TCP enabled and firewall permitted for SQL port, whether default or custom.
'username' => 'your user name here',
'password' => 'your pwd here',
'database' => 'your db here',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'autoinit' => TRUE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE

);

happy coding...

Solution 2

You may want to try odbc driver (built-in db driver in PHP). Mssql driver doesn't come convenient in PHP.

I got this working with CI 2.2.0 connected to MSSQL 2014 database (all Windows platform). I've also tried this using MSSQL 2012 before.

$active_group = 'my_mssql';
$active_record = TRUE;

$db['my_mssql']['hostname'] = 'Driver={SQL Server Native Client 11.0};Server=Host\Instance;Database=queue_sys;';
$db['my_mssql']['username'] = 'wow_queue';
$db['my_mssql']['password'] = 'wow12345';
$db['my_mssql']['database'] = '';
$db['my_mssql']['dbdriver'] = 'odbc';
$db['my_mssql']['dbprefix'] = '';
$db['my_mssql']['pconnect'] = FALSE;
$db['my_mssql']['db_debug'] = TRUE;
$db['my_mssql']['cache_on'] = FALSE;
$db['my_mssql']['cachedir'] = '';
$db['my_mssql']['char_set'] = 'utf8';
$db['my_mssql']['dbcollat'] = 'utf8_general_ci';
$db['my_mssql']['swap_pre'] = '';
$db['my_mssql']['autoinit'] = TRUE;
$db['my_mssql']['stricton'] = FALSE;

Note:

SQL Server Native Client 11.0 or SQL Server Native Client 10.0, just play with both settings.

Server=xx.xx.xx.x usually is in the format Server=Host\Instance enter image description here

Share:
30,855
bennysantoso
Author by

bennysantoso

Updated on July 09, 2022

Comments

  • bennysantoso
    bennysantoso almost 2 years

    My server use Windows Server with MSSQL 2012. While I am use OS X (El Capitan) with XAMPP (Apache) for Mac and develop website using Codeigniter 2.2.0.

    Here is my configuration :

    $active_group = 'my_mssql';
    $active_record = TRUE;
    
    $db['my_mssql']['hostname'] = 'xx.xx.xx.x';
    $db['my_mssql']['username'] = 'wow_queue';
    $db['my_mssql']['password'] = 'wow12345';
    $db['my_mssql']['database'] = 'queue_sys';
    $db['my_mssql']['dbdriver'] = 'mssql';
    $db['my_mssql']['dbprefix'] = '';
    $db['my_mssql']['pconnect'] = TRUE;
    $db['my_mssql']['db_debug'] = TRUE;
    $db['my_mssql']['cache_on'] = FALSE;
    $db['my_mssql']['cachedir'] = '';
    $db['my_mssql']['char_set'] = 'utf8';
    $db['my_mssql']['dbcollat'] = 'utf8_general_ci';
    $db['my_mssql']['swap_pre'] = '';
    $db['my_mssql']['autoinit'] = TRUE;
    $db['my_mssql']['stricton'] = FALSE;
    

    but the results is :

    1
    Are my settings be wrong?

    I just want to be able to connect to that server. Does anyone have any advice on solving this?