Quickstart: Use PHP to query an Azure SQL database

This article demonstrates how to use PHP to connect to an Azure SQL database. You can then use T-SQL statements to query data.


To complete this sample, make sure you have the following prerequisites:

  • An Azure SQL database. You can use one of these quickstarts to create and then configure a database in Azure SQL Database:

    Single database Managed instance
    Create Portal Portal
    PowerShell PowerShell
    Configure Server-level IP firewall rule Connectivity from a VM
    Connectivity from on-site
    Load data Adventure Works loaded per quickstart Restore Wide World Importers
    Restore or import Adventure Works from BACPAC file from GitHub


    The scripts in this article are written to use the Adventure Works database. With a managed instance, you must either import the Adventure Works database into an instance database or modify the scripts in this article to use the Wide World Importers database.

  • PHP-related software installed for your operating system:

    • MacOS, install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.

    • Linux, install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.

    • Windows, install PHP for IIS Express and Chocolatey, then install the ODBC driver and SQLCMD. See Step 1.2 and 1.3.

Get SQL server connection information

Get the connection information you need to connect to the Azure SQL database. You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.

  1. Sign in to the Azure portal.

  2. Navigate to the SQL databases or SQL managed instances page.

  3. On the Overview page, review the fully qualified server name next to Server name for a single database or the fully qualified server name next to Host for a managed instance. To copy the server name or host name, hover over it and select the Copy icon.

Add code to query database

  1. In your favorite text editor, create a new file, sqltest.php.

  2. Replace its contents with the following code. Then add the appropriate values for your server, database, user, and password.

        $serverName = "your_server.database.windows.net"; // update me
        $connectionOptions = array(
            "Database" => "your_database", // update me
            "Uid" => "your_username", // update me
            "PWD" => "your_password" // update me
        //Establishes the connection
        $conn = sqlsrv_connect($serverName, $connectionOptions);
        $tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
             FROM [SalesLT].[ProductCategory] pc
             JOIN [SalesLT].[Product] p
             ON pc.productcategoryid = p.productcategoryid";
        $getResults= sqlsrv_query($conn, $tsql);
        echo ("Reading data from table" . PHP_EOL);
        if ($getResults == FALSE)
            echo (sqlsrv_errors());
        while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
         echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);

Run the code

  1. At the command prompt, run the app.

    php sqltest.php
  2. Verify the top 20 rows are returned and close the app window.

Next steps