Friday, March 13, 2015

Connect CodeIgniter to SQL Server using sqlsrv driver

Specification
1.       Wamp Server 2.0 with PHP 5.2
2.       CodeIgniter 2.1/2.2
3.       SQL Server 2005/2008
Download First
1.       Download sqlsrv dll driver SQLsrv20.exe (download here)
2.       Download SQL Native Client 2008 (Native Client 10) which is compatible for SQL Server 2005/2008 (download here)

How to connect CodeIgniter to SQL Server 2005 using SQLsrv driver
1.       Install / Upgrade your SQL Native Client to 2008 SP3 (Native Client 10), then restart your computer
2.       Stop your Wamp Server to copy dll file
3.       Install or extract  sqlsrv dll files , you don’t have to copy all of the files , choose the files that have same version with your installed PHP version, in this case I will copy
php_pdo_sqlsrv_52_ts_vc6.dll
php _sqlsrv_52_ts_vc6.dll

Then I’ll copy the files into  :
…\wamp\bin\php\php5.2.5\ext
4.       Next , I will edit php.ini , the location of the file is in ..\wamp\bin\apache\apache2.2.8\binand adding these lines in extension configuration section :
extension=php_pdo_sqlsrv_52_ts_vc6.dll
extension=php_sqlsrv_52_ts_vc6.dll
5.       Start the wamp service
6.       Create test.php(not codeigniter file) the file will be used to test the connection (it’s common native PHP Script )
Adjust with your own host/database/user/password setting in SQL Server

<?php
$serverName = "SISINDOTEK\PRIMAVERA"; //serverName\instanceName
$connectionInfo = array( "Database"=>"cisqldb", "UID"=>"sa", "PWD"=>"sa");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

7.  If the Connection established.  You may try to setup your codeigniter database configuration file (database.php) in 
…\wamp\www\yourWebfolder\application\config
8.       Change the configuration such as mention bellowed (Use your own configuration !)
9.       Don’t forget to change the value of  $db['default']['pconnect'] = FALSE;

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'SISINDOTEK\PRIMAVERA';
$db['default']['username'] = 'sa';
$db['default']['password'] = 'sa';
$db['default']['database'] = 'cisqldb';
$db['default']['dbdriver'] = 'sqlsrv';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

Save the file and now you may try to run your codeigniter web that connected to SQL Server by using sqlsrv dll driver

I Hope that can help you guys !, (http://freelance-it-trainer.blogspot.com)






By :

Freelance Inhouse Trainer

0 comments:

Post a Comment

Silahkan isikan comment box untuk komentar Anda..