php查询sql2008数据库操作系统,使用 PHP 进行查询 - Azure SQL Database SQL Managed Instance | Microsoft Docs...
您現在訪問的是微軟AZURE全球版技術文檔網站,若需要訪問由世紀互聯運營的MICROSOFT AZURE中國區技術文檔網站,請訪問 https://docs.azure.cn.
快速入門:使用 PHP 查詢 Azure SQL 數據庫中的數據庫Quickstart: Use PHP to query a database in Azure SQL Database
05/29/2020
本文內容
適用于:
Azure SQL 數據庫
Azure SQL 托管實例
本文演示了如何使用 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:
具有活動訂閱的 Azure 帳戶。An Azure account with an active subscription.
Azure SQL 數據庫或 Azure SQL 托管實例中的數據庫。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:
重要
本文中腳本的編寫目的是使用 Adventure Works 數據庫。The scripts in this article are written to use the Adventure Works database. 使用 SQL 托管實例時,必須將 Adventure Works 數據庫導入一個實例數據庫,或者修改本文中的腳本,以便使用 Wide World Importers 數據庫。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 相關的軟件:PHP-related software installed for your operating system:
MacOS,安裝 PHP、ODBC 驅動程序,然后安裝 PHP Driver for SQL Server。macOS, install PHP, the ODBC driver, then install the PHP Driver for SQL Server.
Linux,安裝 PHP、ODBC 驅動程序,然后安裝 PHP Driver for SQL Server。Linux, install PHP, the ODBC driver, then install the PHP Driver for SQL Server.
Windows:安裝 PHP 和 PHP 驅動程序,然后安裝 ODBC 驅動程序和 SQLCMD。Windows, install PHP and PHP Drivers, then install the ODBC driver and SQLCMD.
獲取服務器連接信息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.
導航到“SQL 數據庫”或“SQL 托管實例”頁 。Navigate to the SQL Databases or SQL Managed Instances page.
在“概述”頁上,在“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
在喜歡的文本編輯器中,創建新文件 sqltest.php。In your favorite text editor, create a new file, sqltest.php.
將其內容替換為以下代碼。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);
}
sqlsrv_free_stmt($getResults);
?>
運行代碼Run the code
在命令提示符下運行此應用。At the command prompt, run the app.
php sqltest.php
驗證是否返回了前 20 行,然后關閉應用窗口。Verify the top 20 rows are returned and close the app window.
后續步驟Next steps
總結
以上是生活随笔為你收集整理的php查询sql2008数据库操作系统,使用 PHP 进行查询 - Azure SQL Database SQL Managed Instance | Microsoft Docs...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三代:集成电路计算机
- 下一篇: oracle连接数一直超出,Oracle