How to Connect MSSQL with php using PDO-SQLSRV

By | November 24, 2014

PHP is Supporting Many Databases. We can connect MSSQL with PHP using the following method. There are many different ways available to connect MSSQL with PHP, here is the tutorial for establishing database connectivity between MSSQL and PHP using PDO.

Step 1: Install the SQL Server related version you need from this link. Click here

Step 2: Install MSSQL Server with developer and change mode of Authentication using mixed SQL Server authentication and put your password there.

Step3: After installing MSSQL add your database and tables.

Step 4: Next you need the SQL Server connecting driver for php this available at here    sqlsrv30.exe

Step 5: After downloading sqlsrv30.exe, extract the file and there will be following files.

MSSQL Connect with php server driver uploading

MSSQL Conncet with php and selecting extension for depending on your db

Step 6: Check your php version using phpinfo(); function and choose two files depending on your version.

  • If your version is 5.3, then copy the following files php_pdo_sqlsrv_53_ts.dll & php_sqlsrv_53_ts.dll  and paste into   Drive Installed:wamp/bin/php/phpversion/ext and paste the files.
  • If your version is 5.4, then copy the following files php_pdo_sqlsrv_54_ts.dll & php_sqlsrv_54_ts.dll  and paste into   Drive Installed:wamp/bin/php/phpversion/ext and paste the files. 

step 7: After  Pasting these two files you need to change the php.ini(wamp\bin\apache\apache2.2.22\bin) file add this line extension=php_sqlsrv_53_ts.dll,
extension=php_pdo_sqlsrv_ts.dll and extension=php_pdo_sqlsrv_53_ts.dll

Mssq connect with php  changing extension

Extension adding for mssql connect

step 8: Now save the php.ini file and restart the wamp server

step 9: PDO to connect with database code given below

[code type=php ]

try
{
$serverName=”SERVER-Name”;//
$conn = new PDO( “sqlsrv:server=$serverName ; Database=db_name”, server-name”, “your_password”);
}

catch(Exception $e)
{
echo $e.”Error in connect with mssql”;
}

[/code]

step 10: If you are using mssql version below 2012 you need to install sql server native client for 2012 download here.

(Find the name sqlncli.msi with microsoft version download and install)

 

Leave a Reply

Your email address will not be published. Required fields are marked *

noncreativity-anemotropism