快速入门:使用 PHP 查询 Azure SQL 数据库中的数据库Quickstart: Use PHP to query a database in Azure SQL Database

适用于:是 Azure SQL 数据库 是Azure SQL 托管实例 APPLIES TO: yesAzure SQL Database yesAzure SQL Managed Instance

本文演示了如何使用 PHP 连接到 Azure SQL 数据库或 Azure SQL 托管实例中的数据库。This article demonstrates how to use PHP to connect to a database in Azure SQL Database or Azure SQL Managed Instance. 然后即可使用 T-SQL 语句来查询数据。You can then use T-SQL statements to query data.

先决条件Prerequisites

若要完成本快速入门,你需要:To complete this quickstart, you need:

获取服务器连接信息Get server connection information

获取连接到 Azure SQL 数据库中的数据库所需的连接信息。Get the connection information you need to connect to the database in Azure SQL Database. 在后续过程中,将需要完全限定的服务器名称或主机名称、数据库名称和登录信息。You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 导航到“SQL 数据库”或“SQL 托管实例”页 。Navigate to the SQL Databases or SQL Managed Instances page.

  3. 在“概述”页上,在“Server 名称”旁查看 Azure SQL 数据库中的数据库的完全限定服务器名称,或在“Host”旁边查看 Azure VM 上的 Azure SQL 托管实例或 SQL Server 的完全限定服务器名称(或 IP 地址) 。On the Overview page, review the fully qualified server name next to Server name for a database in Azure SQL Database or the fully qualified server name (or IP address) next to Host for an Azure SQL Managed Instance or SQL Server in an Azure VM. 若要复制服务器名称或主机名称,请将鼠标悬停在其上方,然后选择“复制”图标。To copy the server name or host name, hover over it and select the Copy icon.

备注

有关 Azure VM 上的 SQL Server 的连接信息,请参阅连接到 SQL Server 实例For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.

添加用于查询数据库的代码Add code to query the database

  1. 在喜欢的文本编辑器中,创建新文件 sqltest.php。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.

    <?php
        $serverName = "your_server.database.chinacloudapi.cn"; // 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);
        }
        sqlsrv_free_stmt($getResults);
    ?>
    

运行代码Run the code

  1. 在命令提示符下运行此应用。At the command prompt, run the app.

    php sqltest.php
    
  2. 验证是否返回了前 20 行,然后关闭应用窗口。Verify the top 20 rows are returned and close the app window.

后续步骤Next steps