Hướng dẫn php microsoft sql query - truy vấn php microsoft sql

Chuyển đến nội dung chính

Trình duyệt này không còn được hỗ trợ nữa.

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Quickstart: Use PHP to query a database in Azure SQL Database

  • Bài viết
  • 08/31/2022
  • 2 phút để đọc

Trong bài viết này

Applies to: Azure SQL Database Azure SQL Managed Instance

Hướng dẫn php microsoft sql query - truy vấn php microsoft sql
Azure SQL Database Azure SQL Managed Instance

This article demonstrates how to use PHP to connect to a database in Azure SQL Database or Azure SQL Managed Instance. You can then use T-SQL statements to query data.

Prerequisites

To complete this quickstart, you need:

  • An Azure account with an active subscription. Create an account for free.

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

    ActionSQL DatabaseSQL Managed InstanceSQL Server on Azure VM
    Create Portal Portal Portal
    CLI CLI
    PowerShell PowerShell PowerShell
    Configure Server-level IP firewall rule Connectivity from a VM
    Connectivity from on-premises Connect to a SQL Server instance
    Load data Adventure Works loaded per quickstart Restore Wide World Importers Restore Wide World Importers
    Restore or import Adventure Works from a BACPAC file from GitHub Restore or import Adventure Works from a BACPAC file from GitHub

    Important

    The scripts in this article are written to use the Adventure Works database. With a SQL 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, cài đặt PHP, trình điều khiển ODBC, sau đó cài đặt trình điều khiển PHP cho SQL Server. Xem Bước 1, 2 và 3., install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.

    • Linux, cài đặt PHP, trình điều khiển ODBC, sau đó cài đặt trình điều khiển PHP cho SQL Server. Xem Bước 1, 2 và 3., install PHP, the ODBC driver, then install the PHP Driver for SQL Server. See Step 1, 2, and 3.

    • Windows, cài đặt trình điều khiển PHP và PHP, sau đó cài đặt trình điều khiển ODBC và SQLCMD. Xem Bước 1.2 và 1.3., install PHP and PHP Drivers, then install the ODBC driver and SQLCMD. See Step 1.2 and 1.3.

Nhận thông tin kết nối máy chủ

Nhận thông tin kết nối bạn cần kết nối với cơ sở dữ liệu trong cơ sở dữ liệu Azure SQL. Bạn sẽ cần tên máy chủ hoặc tên máy chủ, tên cơ sở dữ liệu và thông tin đăng nhập đầy đủ cho các quy trình sắp tới.

  1. Đăng nhập vào cổng thông tin Azure.

  2. Điều hướng đến cơ sở dữ liệu SQL hoặc trang phiên bản được quản lý SQL.SQL Databases or SQL Managed Instances page.

  3. Trên trang tổng quan, xem lại tên máy chủ đủ điều kiện bên cạnh tên máy chủ cho cơ sở dữ liệu trong cơ sở dữ liệu Azure SQL hoặc tên máy chủ (hoặc địa chỉ IP) đủ điều kiện bên cạnh máy chủ cho phiên bản được quản lý Azure SQL hoặc máy chủ SQL trong Azure VM. Để sao chép tên máy chủ hoặc tên máy chủ, di chuột qua nó và chọn biểu tượng sao chép.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.

Thêm mã để truy vấn cơ sở dữ liệu

  1. Trong trình soạn thảo văn bản yêu thích của bạn, hãy tạo một tệp mới, sqltest.php.

  2. Thay thế nội dung của nó bằng mã sau. Sau đó thêm các giá trị phù hợp cho máy chủ, cơ sở dữ liệu, người dùng và mật khẩu của bạn.

     "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);
    ?>
    

Chạy mã

  1. Tại dấu nhắc lệnh, chạy ứng dụng.

    php sqltest.php
    
  2. Xác minh 20 hàng hàng đầu được trả về và đóng cửa sổ ứng dụng.

Bước tiếp theo

  • Thiết kế cơ sở dữ liệu đầu tiên của bạn trong cơ sở dữ liệu Azure SQL
  • Trình điều khiển PHP Microsoft cho SQL Server
  • Báo cáo các vấn đề hoặc đặt câu hỏi
  • Ví dụ logic thử lại: Kết nối kiên cường với Azure SQL với PHP

Phản HồI

Gửi và xem ý kiến ​​ph