簡體   English   中英

從 PHP 應用程序寫入 Azure SQL 數據庫

[英]Writing to Azure SQL Database from PHP application

我想知道是否可以從應用程序中的 PHP 表單直接寫入 azure sql 數據庫。

因此,例如,我可以有一個包含“ID”、“姓名”、“電子郵件”之類的 3 字段表單,然后在提交時將 go 直接輸入數據庫。

我知道這很容易用 mySQL 完成,但我對使用 Azure 有特定要求。

謝謝

是的你可以。

如果您使用的是 linux,您應該:

為 SQL 服務器安裝 Microsoft ODBC 驅動程序

為 Microsoft SQL 服務器安裝 PHP 驅動程序

然后它非常簡單:

<?php
    $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);
    }
    sqlsrv_free_stmt($getResults);
?>

MS 的完整文檔

PHP SQLSRV 函數的完整文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM