Hey guys!
Everything ok?

In this post, I would like to demonstrate how to connect to SQL Server using PHP (Xampp) and the PDO driver on Windows. Many people end up having difficulties installing and configuring the drivers, due to small technical details that end up not being observed and making the connection between PHP and the SQL Server database impossible.

The first step to achieving this connection is download Microsoft® ODBC Driver 18.2.1.1 for SQL Server® to your machine and install the driver.

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

<?php
    phpinfo();
?>

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

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

  • PHP version (in the example case, 7.1.1)
  • PHP architecture (in the example case, x86)
  • Configuration file location (php.ini)
  • Check whether the PHP compilation version is TS (Thread-safe) or NTS (Non Thread-safe) (in the case of the example, it is 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.2To go down
5.10.1PHP 7.4 to PHP 8.1To go down
5.9PHP 7.3 to PHP 8.0To go down
5.8PHP 7.2 to PHP 7.4To go down
4.3PHP 7.0 and PHP 7.1To go down
4.0PHP 7.0To go down
3.2PHP 5.6.4+ or PHP 5.5.16+ or PHP 5.4.32To go down
3.1PHP 5.5.16+ or PHP 5.4.32To go down
3.0PHP 5.4.32 or PHP 5.3.0To go down
2.0PHP 5.3.0 or PHP 5.2.4 or PHP 5.2.13To go down

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

CentOS 7
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

CentOS 7
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

CentOS 7
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

CentOS 7
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
CentOS 7
Ubuntu 15.10 (64-bit)
Ubuntu 16.04 (64-bit)
Debian 8 (64-bit)
Red Hat Enterprise Linux 7 (64-bit)
MacOS 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 2003 Service Pack 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 version of the driver according to your version of PHP, SQL Server and Operating System, download the indicated driver and choose a folder to extract the files during installation.

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

If you don't 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.ini file by searching for Loaded Configuration File

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

Remembering that the name of the DLLs varies according to the PHP version (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 to the php\ext directory.

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

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

If it is the same as in the screenshot, it means that the driver is installed and ready to be used!

Connection Test Script

<?php

try
{

    $servidor = "vm-dba";
    $instancia = "sql2016";
    $porta = 1433;
    $database = "master";
    $usuario = "usuario_php";
    $senha = "123456";
    
    $conexao = new PDO( "sqlsrv:Server={$servidor}\\{$instancia},{$porta};Database={$database}", $usuario, $senha );
}
catch ( PDOException $e )
{
    echo "Drivers disponiveis: " . implode( ",", PDO::getAvailableDrivers() );
    echo "\nErro: " . $e->getMessage();
    exit;
}

$query = $conexao->prepare( "select @@version" );
$query->execute();

$resultado = $query->fetchAll();

echo $resultado['0']['0'];

unset( $conexao );
unset( $query );

Result:

That's it!
I hope you enjoyed this article and you can now start creating 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.

Big hug!