Disclosure: This post may contain affiliate links, meaning I get a commission if you decide to make a purchase through my links, at no cost to you. Please read my disclosure for more info.
Hi Guys, welcome back to codingofcents.com. Today we will learn how to connect PHP to Microsoft SQL Server with ODBC Driver.
To connect PHP to Microsoft SQL Server is need more effort and motivation because we need to install Microsoft ODBC driver and also PHP sqlsrv driver.
To follow this tutorial, you need to install PHP, Microsoft ODBC Driver, and PHP Sqlsrv driver. You can read full System Requirements for the Microsoft Drivers for PHP for SQL Server in this link.
In this tutorial, I will use PHP 7.2.11 which include when I install Laragon local server (similar to xampp) and Microsoft ODBC Driver 13. You can download them by clicking links below:
- Install Laragon
First, we must install the local server to serve our PHP application to make our application run properly. You can find the installation process in my previous post
- Install SQL Server ODBC Driver
Double click the Microsoft ODBC Driver for SQL Server installation file version which sustainable with your windows version (32/64 bit) then click next
- Install SQL Server ODBC Driver Step 2
- Install SQL Server ODBC Driver Step 3
- Install SQL Server ODBC Driver Step 4
Wait for installation
- Install SQL Server ODBC Driver Step 5
- Install Microsoft driver for PHP for SQL Server
After install ODBC driver now we need to install driver PHP for SQL Server which downloaded previously. Double click the installation file and click Yes
- Install Microsoft driver for PHP for SQL Server Step 2
Click Browse to decide where you want to place the extracted files. Then click OK. After that
- Copy .DDL file
After the file is extracted, copy two files below to extension folder where your PHP installed. For example C:\laragon\bin\php\php-7.2.11-Win32-VC15-x64\ext.
- Edit php.ini file
After copy PHP extension folder, we need to edit our php.ini file to active the extension. I just rename the two files that had been copied. Edit php.ini by add to line code like below. extension= php_sqlsrv_72_ts.dll extension= php_pdo_sqlsrv_72_ts.dll After that we need to restart the apache server
- Test the installation
We can test the driver installation by open CMD and type php -v
- We can also check installation by go to http://localhost:808/?q=info
Note: I change apache port to 808, so if you do not change the port just got to http://localhost?q=info and you get your PHP information. Search for sqlsrv. If you find it, congrats you are done this lesson. if you find an error please check back the installation process
PHP can be integrated with Microsoft SQL Server with the driver that provided by Microsoft and it not hard to install.