How to connect to SQL Server using PHP (Xampp) and PDO driver in Windows

Views: 16.411
Reading Time: 3 minutes

Hello everybody!
All right?

In this post, I would like to demonstrate how to connect to SQL Server using PHP (Xampp) and PDO driver on Windows. Many people find it difficult to install and configure the drivers due to small technical details that go unnoticed and make it impossible to connect PHP to the SQL Server database.

The first step to getting this connection is download Microsoft® ODBC Driver 13.1 for SQL Server® to your machine and perform driver installation.

The next step is to view the information of your environment. To do this, create a file called phpinfo.php in the root of your PHP web server (default directory is C: \ xampp \ htdocs), with the content below:

After viewing this file in your web browser (http: //localhost/phpinfo.php), you should see a screen like this:

In this screen, we need to identify the following environment variables:

  • PHP version (in this case, 7.1.1)
  • PHP architecture (in the example case, x86)
  • Configuration File Location (php.ini)
  • Check if PHP build version is thread safe (TS) or non thread safe (NTS) (in this example, it's TS)

After identifying this information, let's now download Microsoft Drivers for PHP for SQL Server, according to your version of PHP:

Relationship between driver version and PHP version

Driver VersionPHP versionDriver Download
4.3PHP 7.0 and PHP 7.1Download
4.0PHP 7.0Download
3.2PHP 5.6.4 + or PHP 5.5.16 + or PHP 5.4.32Download
3.1PHP 5.5.16 + or PHP 5.4.32Download
3.0PHP 5.4.32 or PHP 5.3.0Download
2.0PHP 5.3.0 or PHP 5.2.4 or PHP 5.2.13Download

Relationship between driver version and ODBC driver version

Driver VersionODBC Driver or SQL Server Native Client Driver Version
4.3Microsoft ODBC 11 Driver for SQL Server
ou Microsoft ODBC 13.1 Driver for SQL Server
4.0Microsoft ODBC 11 Driver for SQL Server
ou Microsoft ODBC 13 Driver for SQL Server
3.2 or 3.1Microsoft ODBC 11 Driver for SQL Server
3.0SQL Server 2012 Feature Pack
2.0Microsoft SQL Server 2008 R2 Native Client 32 bits (X86)
ou Microsoft SQL Server 2008 R2 Native Client 64 bits (x64)

Relationship between driver version and database version

Driver VersionDatabase Version
4.3SQL Server 2008 R2 and Above
4.0SQL Server 2008 and above
3.1SQL Server 2008 and above
2.0 and 3.0SQL Server 2005 and above

Relationship between driver version and Windows version

Driver VersionOperating System Version
4.3Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
Ubuntu 15.10 (64-bit)
Ubuntu 16.04 (64-bit)
Debian 8 (64-bit)
Red Hat Enterprise Linux 7 (64-bit)
Mac OS Sierra (64-bit)
Mac OS El Capitan (64-bit)
4.0Windows Server 2008 SP2
Windows Server 2008 R2 SP1
Windows Server 2012
Windows Server 2012 R2
Windows Vista SP2
Windows 7 SP1
Windows 8
Windows 8.1
Windows 10
Ubuntu 15.04 (64-bit)
Ubuntu 16.04 (64-bit)
Red Hat Enterprise Linux 7 (64-bit)
3.2 and 3.1Windows Server 2008 R2 SP1
Windows Vista SP2
Windows Server 2008 SP2
Windows 7 SP1
Windows Server 2012
Windows Server 2012 R2
Windows 8
Windows 8.1
3.0Windows Server 2008 R2 SP1
Windows Vista SP2
Windows Server 2008 SP2
Windows 7 SP1
2.0Windows Server Service Pack 2003 1
Windows XP Service Pack 3
Windows Vista Service Pack 1 or later
Windows Server 2008
Windows Server 2008 R2
Windows 7

After identifying the correct driver version according to your version of PHP, SQL Server, and Operating System, download the indicated driver and choose a folder to extract the files to during installation.

After that, copy all the files that were installed to the directory. php \ ext on your Xampp installation (default directory is C: \ xampp \ php \ ext)

If you do not know the path of your configuration file (the default path is C: \ xampp \ php \ php.ini), in the phpinfo () screen shown above, you can identify the correct path of your php file. ini looking for Loaded Configuration File

After copying all files, edit the php.ini file from your installation and add the following records:
extension=php_pdo_sqlsrv_71_ts_x86.dll
extension=php_sqlsrv_71_ts_x86.dll

Remember that the names of the DLLs vary according to the PHP version (71, 70, 54, etc.), Thread-safe (TS or NTS) and architecture (X86 or X64) and should ALWAYS be the same as the file names you have. downloaded and copied to the php \ ext directory.

Once you have edited the php.ini file, simply restart Apache for the changes to take effect, as shown below:

Open the phpinfo.php file (http: //localhost/phpinfo.php) and see if the driver has now been loaded:

If it is the same as print, it means that the driver is installed and ready to use!

Connection Test Script

Result:

That's it!
I hope you enjoyed this article and can now start building applications using PHP and SQL Server!

There is also a very interesting link from Microsoft itself that helps developers create a PHP environment. To access this documentation, click this link here.

Strong hug!