Monday, February 17, 2014

How to Connect PHP with SQL Server from Windows Env using SQLSRV?

Here we are going to install SQLSRV Driver and configure with PHP,

1 - Before Start, Run phpinfo() from your localhost/server, and find your System bit (x86/x64). From my research SQLSRV will work only on Wamp2.2 and x86 Env.
2 - Now open your wamp php.ini, and enter below lines under extension section,
    extension=php_sqlsrv_52_nts.dll;
    extension=php_sqlsrv_52_ts_vc6.dll;
3 - Then download SQLSRV driver from http://www.microsoft.com/en-us/download/details.aspx?id=20098
4 - Run SQLSRV driver and enter your original PHP ext path(E:\wamp\bin\php\php5.3.13\ext) also please make sure the same path given in php.ini file(;extension_dir = "ext"
extension_dir = "e:/wamp/bin/php/php5.3.13/ext/")
5 - Once you successfully installed SQLSRV, please make sure the above given dll files loaded under above PATH.
6 - Its time to restart your wamp server(several times if needed)
7 - Now check with phpinfo(), sqlsrv should be list in your phpinfo information
8 - Then Finally you must download SQLCLIENT for SQLSRV driver from
SQL Server Native Clients are here:
SQL 2012
  http://www.microsoft.com/download/en/details.aspx?id=29065
SQL 2008 R2
  http://www.microsoft.com/download/en/details.aspx?id=16978
SQL 2005
  http://www.microsoft.com/download/en/details.aspx?id=20101
 
 9 - Finally try with below codes from your PHP to test with PHP SQLSRV driver,


 $dbhost = "servername";
 $user = "sq";
 $pwd="xxxx";

$link = sqlsrv_connect($dbhost, $user, $pwd);
if($link === FALSE) {
    echo 'Could not connect';
    die('Could not connect: ' . sqlsrv_errors(SQLSRV_ERR_ALL));
}
echo 'Successful connection';
sqlsrv_close($link);
?>

10. Now your Wamp server start to work with SQLSRV driver.

No comments: