Hello,
Recently I was working on a project in which requirement was to get data from Microsoft Sql Server from a PHP script. So this blog is about accessing MS Sql server from PHP using PDO object.
First thing is to add necessary extensions. Go to following site and download drivers for MS SQL server.
http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Once you extract it it will have number of dll files Now you have to identify correct DLL files for your system. This drivers are compatible with PHP version 5.3 . Now go to your XAMPP installation and search for php.dll
It will display correct PHP dll you have.
1) If you have php.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.
1) If you have php.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll
php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll
Normally it should be VC 9 files.
Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.
extension=php_sqlsrv_53_nts_vc9.dll
extension= php_pdo_sqlsrv_53_nts_vc9 .dll
Save the ini files and restart you Apache from either XAMPP control panel or from Windows Services Console.
After that you can check php info as follow.
<?php
phpinfo();
?>
Check the pdo support. It should display sqlsrv now. That means modules are loaded successfully and drivers are available now. Use following code to connect to MS SQL server using sqlsrv
<?php
$serverName = " serverName\instanceName ";
$connection = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connection);
if( $conn ) {
echo "Connection established.
";
}else{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}
?>
That's it and you should be able to query Microsoft Sql Server now. Hope this helps you. This works with Microsoft Sql Server 2005, 2008 and 2008 R2.
Recently I was working on a project in which requirement was to get data from Microsoft Sql Server from a PHP script. So this blog is about accessing MS Sql server from PHP using PDO object.
First thing is to add necessary extensions. Go to following site and download drivers for MS SQL server.
http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx
Once you extract it it will have number of dll files Now you have to identify correct DLL files for your system. This drivers are compatible with PHP version 5.3 . Now go to your XAMPP installation and search for php.dll
It will display correct PHP dll you have.
1) If you have php.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc9.dll
php_pdo_sqlsrv_53_nts_vc9.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_ts_vc9.dll
php_pdo_sqlsrv_53_ts_vc9.dll
above files should be used if your PHP version is compiled with Visual C++ 9.0 . Else following files should be used.
1) If you have php.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_nts_vc6.dll
php_pdo_sqlsrv_53_nts_vc6.dll
2) If you have php5ts.dll then move following files to xampp/php/ext directory.
php_sqlsrv_53_ts_vc6.dll
php_pdo_sqlsrv_53_ts_vc6.dll
Normally it should be VC 9 files.
Now we have to load files that we added recently. Open the php ini file and add entry in the area of dynamic extensions as follow.
extension=php_sqlsrv_53_nts_vc9.dll
extension= php_pdo_sqlsrv_53_nts_vc9 .dll
Save the ini files and restart you Apache from either XAMPP control panel or from Windows Services Console.
After that you can check php info as follow.
<?php
phpinfo();
?>
Check the pdo support. It should display sqlsrv now. That means modules are loaded successfully and drivers are available now. Use following code to connect to MS SQL server using sqlsrv
<?php
$serverName = " serverName\instanceName ";
$connection = array( "Database"=>"dbName");
$conn = sqlsrv_connect( $serverName, $connection);
if( $conn ) {
echo "Connection established.
";
}else{
echo "Connection could not be established.
";
die( print_r( sqlsrv_errors(), true));
}
?>
That's it and you should be able to query Microsoft Sql Server now. Hope this helps you. This works with Microsoft Sql Server 2005, 2008 and 2008 R2.