Download the Microsoft Drivers for PHP for SQL Server from Microsoft: https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver16
Extract the following files based on the version of PHP you have installed. Mine was PHP 8.2.1
- php_pdo_sqlsrv_82_ts_x64.dll
- php_sqlsrv_82_ts_x64.dll
Copy these files to the php ext folder
Add the following extensions to the php.ini file:
- extension=php_pdo_sqlsrv_82_ts_x64.dll
- extension=php_sqlsrv_82_ts_x64.dll
Enable the following extensions:
- extension=odbc
- extension=pdo_odbc
Make the connection:
$conn = new PDO("sqlsrv:Server=localhost;Database=$DatabaseName", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'PDO SQL Server Connection successful</br>';