php查询sql2008数据库操作系统,使用 PHP 进行查询 - Azure SQL Database SQL Managed Instance | Microsoft Docs...
您現(xiàn)在訪問的是微軟AZURE全球版技術(shù)文檔網(wǎng)站,若需要訪問由世紀(jì)互聯(lián)運(yùn)營的MICROSOFT AZURE中國區(qū)技術(shù)文檔網(wǎng)站,請(qǐng)?jiān)L問 https://docs.azure.cn.
快速入門:使用 PHP 查詢 Azure SQL 數(shù)據(jù)庫中的數(shù)據(jù)庫Quickstart: Use PHP to query a database in Azure SQL Database
05/29/2020
本文內(nèi)容
適用于:
Azure SQL 數(shù)據(jù)庫
Azure SQL 托管實(shí)例
本文演示了如何使用 PHP 連接到 Azure SQL 數(shù)據(jù)庫或 Azure SQL 托管實(shí)例中的數(shù)據(jù)庫。This article demonstrates how to use PHP to connect to a database in Azure SQL Database or Azure SQL Managed Instance. 然后即可使用 T-SQL 語句來查詢數(shù)據(jù)。You can then use T-SQL statements to query data.
先決條件Prerequisites
若要完成本快速入門,你需要:To complete this quickstart, you need:
具有活動(dòng)訂閱的 Azure 帳戶。An Azure account with an active subscription.
Azure SQL 數(shù)據(jù)庫或 Azure SQL 托管實(shí)例中的數(shù)據(jù)庫。A database in Azure SQL Database or Azure SQL Managed Instance. 可以根據(jù)下述快速入門之一,創(chuàng)建數(shù)據(jù)庫,然后對(duì)其進(jìn)行配置:You can use one of these quickstarts to create and then configure a database:
重要
本文中腳本的編寫目的是使用 Adventure Works 數(shù)據(jù)庫。The scripts in this article are written to use the Adventure Works database. 使用 SQL 托管實(shí)例時(shí),必須將 Adventure Works 數(shù)據(jù)庫導(dǎo)入一個(gè)實(shí)例數(shù)據(jù)庫,或者修改本文中的腳本,以便使用 Wide World Importers 數(shù)據(jù)庫。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.
已為操作系統(tǒng)安裝與 PHP 相關(guān)的軟件:PHP-related software installed for your operating system:
MacOS,安裝 PHP、ODBC 驅(qū)動(dòng)程序,然后安裝 PHP Driver for SQL Server。macOS, install PHP, the ODBC driver, then install the PHP Driver for SQL Server.
Linux,安裝 PHP、ODBC 驅(qū)動(dòng)程序,然后安裝 PHP Driver for SQL Server。Linux, install PHP, the ODBC driver, then install the PHP Driver for SQL Server.
Windows:安裝 PHP 和 PHP 驅(qū)動(dòng)程序,然后安裝 ODBC 驅(qū)動(dòng)程序和 SQLCMD。Windows, install PHP and PHP Drivers, then install the ODBC driver and SQLCMD.
獲取服務(wù)器連接信息Get server connection information
獲取連接到 Azure SQL 數(shù)據(jù)庫中的數(shù)據(jù)庫所需的連接信息。Get the connection information you need to connect to the database in Azure SQL Database. 在后續(xù)過程中,將需要完全限定的服務(wù)器名稱或主機(jī)名稱、數(shù)據(jù)庫名稱和登錄信息。You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.
導(dǎo)航到“SQL 數(shù)據(jù)庫”或“SQL 托管實(shí)例”頁 。Navigate to the SQL Databases or SQL Managed Instances page.
在“概述”頁上,在“Server 名稱”旁查看 Azure SQL 數(shù)據(jù)庫中的數(shù)據(jù)庫的完全限定服務(wù)器名稱,或在“Host”旁邊查看 Azure VM 上的 Azure SQL 托管實(shí)例或 SQL Server 的完全限定服務(wù)器名稱(或 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. 若要復(fù)制服務(wù)器名稱或主機(jī)名稱,請(qǐng)將鼠標(biāo)懸停在其上方,然后選擇“復(fù)制”圖標(biāo)。To copy the server name or host name, hover over it and select the Copy icon.
備注
有關(guān) Azure VM 上的 SQL Server 的連接信息,請(qǐng)參閱連接到 SQL Server 實(shí)例。For connection information for SQL Server on Azure VM, see Connect to a SQL Server instance.
添加用于查詢數(shù)據(jù)庫的代碼Add code to query the database
在喜歡的文本編輯器中,創(chuàng)建新文件 sqltest.php。In your favorite text editor, create a new file, sqltest.php.
將其內(nèi)容替換為以下代碼。Replace its contents with the following code. 然后,為服務(wù)器、數(shù)據(jù)庫、用戶和密碼添加相應(yīng)的值。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);
?>
運(yùn)行代碼Run the code
在命令提示符下運(yùn)行此應(yīng)用。At the command prompt, run the app.
php sqltest.php
驗(yàn)證是否返回了前 20 行,然后關(guān)閉應(yīng)用窗口。Verify the top 20 rows are returned and close the app window.
后續(xù)步驟Next steps
總結(jié)
以上是生活随笔為你收集整理的php查询sql2008数据库操作系统,使用 PHP 进行查询 - Azure SQL Database SQL Managed Instance | Microsoft Docs...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三代:集成电路计算机
- 下一篇: oracle连接数一直超出,Oracle