Permalink
Fetching contributors…
Cannot retrieve contributors at this time
88 lines (65 sloc) 3.71 KB
title description services author manager ms.service ms.custom ms.devlang ms.topic ms.date ms.author
Use PHP to query Azure SQL Database | Microsoft Docs
This topic shows you how to use PHP to create a program that connects to an Azure SQL Database and query it using Transact-SQL statements.
sql-database
CarlRabeler
craigg
sql-database
mvc,develop apps
php
quickstart
04/01/2018
carlrab

Use PHP to query an Azure SQL database

This quickstart demonstrates how to use PHP to create a program to connect to an Azure SQL database and use Transact-SQL statements to query data.

Prerequisites

To complete this quickstart, make sure you have the following:

[!INCLUDE prerequisites-create-db]

  • A server-level firewall rule for the public IP address of the computer you use for this quickstart.

  • You have installed PHP and related software for your operating system:

    • MacOS: Install Homebrew and PHP, install the ODBC driver and SQLCMD, and then install the PHP Driver for SQL Server. See Steps 1.2, 1.3, and 2.1.
    • Ubuntu: Install PHP and other required packages, and then install the PHP Driver for SQL Server. See Steps 1.2 and 2.1.
    • Windows: Install the newest version of PHP for IIS Express, the newest version of Microsoft Drivers for SQL Server in IIS Express, Chocolatey, the ODBC driver, and SQLCMD. See Steps 1.2 and 1.3.

SQL server connection information

[!INCLUDE prerequisites-server-connection-info]

Insert code to query SQL database

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

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

    <?php
    $serverName = "your_server.database.windows.net";
    $connectionOptions = array(
        "Database" => "your_database",
        "Uid" => "your_username",
        "PWD" => "your_password"
    );
    //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);
    }
    sqlsrv_free_stmt($getResults);
    ?>

Run the code

  1. At the command prompt, run the following commands:

    php sqltest.php
  2. Verify that the top 20 rows are returned and then close the application window.

Next steps