In this this tutorial we will go to learn that how to configure PHP for SQL Server and connect PHP with MS SQL Server.
In many cases we face problem that how to connect PHP with SQL server since Microsoft SQL Server Drivers for PHP varies version to version of PHP.
Step
1: First you have to check that which version of
PHP you have configured in your PC. Most of the PHP version comes with
Microsoft SQL Server Drivers for PHP. If your PHP have drivers for SQL Server
then it is OK otherwise you have to configure it first. First Check your phpinfo() if you are using XAMPP server
and if you are using IIS server then you can check it from PHP Manager. Here I
am checking it manually for all types of WEB Server.
Step
2: Create a phpinfo folder in your web root folder -> Create
an index.php file. Type the following Code.
<?php phpinfo(); ?>
Step 3: Type the following URL into your
browser: http://localhost/phpinfo
In My
Computer I have PHP version 5.4.24. Now scroll down and check that driver for
sqlsrv or mssql is there or not. As shown in the figure.
After checking this that your PHP is configured with SQL Server drivers then skip step 4 to step 8 otherwise go to step 4 that how can you configure it for SQL Server.
NOTE: The
SQL Driver doesn't (officially) exist yet for PHP 5.5. If you have PHP
5.5.x version then I recommend converting PHP 5.3.x or PHP 5.4.x version to
connect with SQL server.
Step 4: First you have to download the drivers to connect PHP with MS SQL
Server. You can download it from here http://www.microsoft.com/en-in/download/details.aspx?id=20098.
Choose SQLSRV30.EXE for PHP 5.3.x or PHP 5.4.x versions.
Step
5: After
downloading SQLSRV30.EXE extract it
to a folder you will get the dll files. As shown in the figure.
Step 6: Now try to choose correct dll
files. If you are using PHP 5.4.x then choose php_pdo_sqlsrv_54_ts.dll and php_sqlsrv_54_ts.dll
otherwise choose as per your PHP version.
Step 7: Copy these dll files and paste it
into your PHP extension folder. In my case the path is shown in the figure. But it
may be different on XAMPP and WAMP. So go to your PHP ext folder and paste the
dll files there.
Step 8: Now open php.ini file in notepad
and go to Dynamic Extensions Section and paste these two lines.
extesion=php_sqlsrv_54_ts.dll
extension=php_pdo_sqlsrv_54_ts.dll
As it is
shown in the figure.
Step 8: Now restart your web server (Apache
or IIS which one you are using)
Step 9: Create a phpsqlserver
folder in your web root folder -> Create an index.php file. Type the following Code for database connectivity use your server name, user id and password instead of the given credentials in the figure.