Click on the banner to learn about and purchase my database training on Azure

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

Views: 44.130 views
Reading Time: 4 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 18.2.1.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
5.11.0-beta1PHP 7.4 to PHP 8.2Download
5.10.1PHP 7.4 to PHP 8.1Download
5.9PHP 7.3 to PHP 8.0Download
5.8PHP 7.2 to PHP 7.4Download
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

Click here to expand

Relationship between driver version and database version

Click here to expand
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

Click here to expand
Driver VersionOperating System Version
5.11Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
Windows 11

7 CentOS
Ubuntu 20.04, 20.10 & 21.04, 21.10
Debian 9, 10 & 11
Red Hat Enterprise Linux 7 & 8
SUSE 12 & 15

macOS Catalina, Mojave, Big Sur & Monterey
(Support Apple M1 ARM64)
5.10.1Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
Windows 11

7 CentOS
Ubuntu 20.04, 20.10, 21.04 & 21.10
Debian 9, 10 & 11
Red Hat Enterprise Linux 7 & 8
SUSE 12 & 15

macOS Catalina, Mojave, Big Sur & Monterey
(Support Apple M1 ARM64)
5.9Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
Windows 11

7 CentOS
Ubuntu 16.04, 20.04 & 20.10
Debian 9 & 10
Red Hat Enterprise Linux 7 & 8
SUSE 12 & 15

macOS Catalina, Mojave & Big Sur
5.8Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
Windows 11

7 CentOS
Ubuntu 16.04 & 19.10
Debian 8 & 9 & 10
Red Hat Enterprise Linux 7 & 8
SUSE 12 & 15

macOS High Sierra, Catalina & Mojave
4.3Windows Server 2012
Windows Server 2012 R2
Windows Server 2016
Windows 8
Windows 8.1
Windows 10
7 CentOS
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

Remembering that the name of the DLL's varies according to the version of PHP (71, 70, 54, etc.), Thread-safe (TS or NTS) and architecture (X86 or X64) and must ALWAYS be the same as the name of the files you downloaded and copied it 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!