Uređivanje

Quickstart: Connect and query with the Microsoft Drivers for PHP for SQL Server

Use this quickstart to install the PHP drivers, connect to Azure SQL with your Microsoft Entra identity, and run a parameterized Transact-SQL (T-SQL) query against the AdventureWorksLT sample data. Choose either the SQLSRV procedural API or the PDO_SQLSRV API. Both samples read connection settings from environment variables and return the same result.

The query reads product data and doesn't create database objects.

Before you start

1. Install PHP and the drivers

Select your operating system. Copy the entire command block, paste it into the specified terminal, and run it.

Open PowerShell as an administrator. Copy and run this block:

winget install --exact --id PHP.PHP.8.5 --source winget --accept-package-agreements --accept-source-agreements
winget install --exact --id Microsoft.msodbcsql.18 --source winget --accept-package-agreements --accept-source-agreements

$env:Path = [Environment]::GetEnvironmentVariable("Path", "Machine") + ";" +
    [Environment]::GetEnvironmentVariable("Path", "User")
New-Item -ItemType Directory -Force C:\php-quickstart | Out-Null
Set-Location C:\php-quickstart

$phpDirectory = Split-Path (Get-Command php.exe -ErrorAction Stop).Source
$phpIni = Join-Path $phpDirectory "php.ini"
if (-not (Test-Path $phpIni)) {
    Copy-Item (Join-Path $phpDirectory "php.ini-development") $phpIni
}

$configuration = [System.IO.File]::ReadAllText($phpIni)
$configuration = $configuration -replace '(?m)^\s*;\s*extension_dir\s*=\s*"ext"\s*$', 'extension_dir = "ext"'
$configuration = $configuration -replace '(?m)^\s*;\s*extension\s*=\s*openssl\s*$', 'extension=openssl'
$configuration = $configuration -replace '(?m)^\s*;\s*extension\s*=\s*zip\s*$', 'extension=zip'
[System.IO.File]::WriteAllText($phpIni, $configuration)

Invoke-WebRequest https://github.com/php/pie/releases/latest/download/pie.phar -OutFile pie.phar
php .\pie.phar install microsoft/sqlsrv
php .\pie.phar install microsoft/pdo_sqlsrv

php --version
php --ri sqlsrv
php --ri pdo_sqlsrv

The last three commands display the installed PHP and extension versions. Close the administrator window after they succeed.

If either php --ri command reports that the extension isn't present, go to Installation troubleshooting before you continue.

2. Set the connection information

Replace <server> and <database> in the block for your operating system. Copy and run the entire block in the same terminal that you'll use to run PHP.

Set-Location C:\php-quickstart
$env:SQL_SERVER = "tcp:<server>.database.windows.net,1433"
$env:SQL_DATABASE = "<database>"
$env:SQL_AUTHENTICATION = "ActiveDirectoryIntegrated"

Important

Both samples enable encryption and validate the server certificate. If your server uses a certificate from a private certificate authority (CA), install the issuing root and intermediate CA certificates in the client operating system trust store. Set SQL_SERVER to a name in the certificate's Subject Alternative Name or Common Name. Otherwise, the connection fails before the query runs. For more information, see Certificate chain not trusted and Configure SQL Server encryption.

TrustServerCertificate=true bypasses server identity validation. Use it only to diagnose an isolated local test environment, not as a trust configuration for production or shared environments.

3. Create and run a sample

Select one PHP API. Create the named file by using the code in the selected tab, and then run the command after the code.

Create quickstart-sqlsrv.php with the following code:

<?php
declare(strict_types=1);

$server = getenv('SQL_SERVER') ?: null;
$database = getenv('SQL_DATABASE') ?: null;
$authentication = getenv('SQL_AUTHENTICATION') ?: null;

if ($server === null || $database === null || $authentication === null) {
    throw new RuntimeException('Set SQL_SERVER, SQL_DATABASE, and SQL_AUTHENTICATION.');
}
if (!in_array($authentication, ['SqlPassword', 'ActiveDirectoryIntegrated', 'ActiveDirectoryMsi'], true)) {
    throw new RuntimeException(
        'Set SQL_AUTHENTICATION to SqlPassword, ActiveDirectoryIntegrated, or ActiveDirectoryMsi.'
    );
}

$user = null;
$password = null;
if ($authentication === 'SqlPassword') {
    $user = getenv('SQL_USER') ?: null;
    $password = getenv('SQL_PASSWORD') ?: null;
    if ($user === null || $password === null) {
        throw new RuntimeException('Set SQL_USER and SQL_PASSWORD for SqlPassword authentication.');
    }
}

$options = [
    'Database' => $database,
    'Authentication' => $authentication,
    'Driver' => 'ODBC Driver 18 for SQL Server',
    'Encrypt' => true,
    'TrustServerCertificate' => false,
];
if ($authentication === 'SqlPassword') {
    $options['UID'] = $user;
    $options['PWD'] = $password;
}

$connection = sqlsrv_connect($server, $options);
if ($connection === false) {
    throw new RuntimeException(print_r(sqlsrv_errors(), true));
}

$sql = <<<'SQL'
SELECT TOP (5) ProductID, Name
FROM SalesLT.Product
WHERE ProductID > ?
ORDER BY ProductID;
SQL;
$parameters = [0];
$statement = sqlsrv_query($connection, $sql, $parameters);
if ($statement === false) {
    $errors = sqlsrv_errors();
    sqlsrv_close($connection);
    throw new RuntimeException(print_r($errors, true));
}

$rows = [];
while (($row = sqlsrv_fetch_array($statement, SQLSRV_FETCH_ASSOC)) !== null) {
    if ($row === false) {
        $errors = sqlsrv_errors();
        sqlsrv_free_stmt($statement);
        sqlsrv_close($connection);
        throw new RuntimeException(print_r($errors, true));
    }
    $rows[] = $row;
}
sqlsrv_free_stmt($statement);
sqlsrv_close($connection);

if (count($rows) !== 5) {
    throw new RuntimeException('Unexpected query result.');
}
$previousProductId = 0;
foreach ($rows as $row) {
    if ($row['ProductID'] <= $previousProductId || $row['Name'] === '') {
        throw new RuntimeException('Unexpected query result.');
    }
    $previousProductId = $row['ProductID'];
}
printf("%-12s%s\n", 'Product ID', 'Name');
printf("%-12s%s\n", '----------', '----');
foreach ($rows as $row) {
    printf("%-12d%s\n", $row['ProductID'], $row['Name']);
}

Run the sample:

php quickstart-sqlsrv.php

4. Verify the result

The product rows can vary by AdventureWorksLT version. Both samples return output that resembles this example:

Product ID  Name
----------  ----
680         HL Road Frame - Black, 58
706         HL Road Frame - Red, 58
707         Sport-100 Helmet, Red
708         Sport-100 Helmet, Black
709         Mountain Bike Socks, M

Each sample checks that the query returned five products with nonempty names and ascending product IDs before it prints the rows. It then releases the statement and closes the connection. The query doesn't leave any database objects or data to remove.

Use another authentication method

The samples also accept managed identity and SQL Server authentication without changing the PHP files.

Managed identity

For an application hosted in Azure, enable a managed identity and create its database user. Set the server, database, and authentication mode in the application's configuration:

SQL_SERVER=tcp:<server>.database.windows.net,1433
SQL_DATABASE=<database>
SQL_AUTHENTICATION=ActiveDirectoryMsi

Don't set SQL_USER or SQL_PASSWORD.

For SQL database in Microsoft Fabric, grant the identity Read item permission through Fabric access controls. Use the SQL connection endpoint from the database item, not the SQL analytics endpoint. SQL database in Fabric doesn't support SQL authentication.

SQL Server authentication

Use SQL Server authentication only for a SQL Server instance that you control, such as an isolated local development container. Keep credentials in application configuration or a secret store. Don't commit them to source control.

$env:SQL_SERVER = "tcp:<server>,1433"
$env:SQL_DATABASE = "<database>"
$env:SQL_AUTHENTICATION = "SqlPassword"
$env:SQL_USER = "<user_id>"
$env:SQL_PASSWORD = "<password>"

For other Microsoft Entra authentication methods, see Connect using Microsoft Entra authentication.

Installation troubleshooting

Use these checks if the installation block stops or a verification command fails.

Get-Command php.exe
php --ini
php --ri sqlsrv
php --ri pdo_sqlsrv
Get-OdbcDriver -Name "ODBC Driver 18 for SQL Server"

If Get-Command can't find php.exe, close every terminal, open a new PowerShell window, and run the checks again. If a php --ri command reports Extension not present, rerun the PIE installation commands from C:\php-quickstart.

If a connection reports FA001 and says that the Authentication option can't be used with Integrated Security, confirm that SQL_AUTHENTICATION is exactly ActiveDirectoryIntegrated. Run php --ri sqlsrv or php --ri pdo_sqlsrv to check the PHP driver version, and update the driver if it's older than 5.10.1.

Clean up

The samples release their statements and close their connections. They don't create database objects or persist data.

The connection settings apply to the current terminal session. Close the terminal when you finish.

For production retry, timeout, logging, and failover settings, use the production baseline instead of extending this first-run sample.