這個快速入門說明如何透過 PHP 應用程式連接 適用於 PostgreSQL 的 Azure 資料庫。 它會顯示如何使用 SQL 陳述式來查詢、插入、更新和刪除資料庫中的資料。 本文的步驟假設你熟悉使用 PHP 開發,且剛開始使用 適用於 PostgreSQL 的 Azure 資料庫。
先決條件
本快速入門會使用 在建立適用於 PostgreSQL 的 Azure 資料庫 中建立的資源作為起點。
安裝 PHP
在您的伺服器上安裝 PHP,或建立包含 PHP 的 Azure Web 應用程式 。
窗戶
- 下載 PHP 7.1.4 非線程安全 (x64) 版本。
- 安裝 PHP 並參考 PHP 手冊以便進一步設定。
- 程式碼使用 PHP 安裝中包含的 pgsql 類別(ext/php_pgsql.dll)。
- 透過編輯 php.ini 設定檔(通常位於 )。 來啟用
C:\Program Files\PHP\v7.1\php.ini擴充功能。 組態檔應包含具有extension=php_pgsql.so文字的一行。 如果沒有顯示,請加入該文字並儲存檔案。 如果文字存在,但加上分號前置詞做為註解,請移除分號以取消文字註解。
Linux(烏班圖)
- 下載 PHP 7.1.4 非線程安全 (x64) 版本。
- 安裝 PHP 並參考 PHP 手冊以便進一步設定。
- 程式碼會使用 pgsql 類別 (php_pgsql.so)。 安裝方式是執行
sudo apt-get install php-pgsql。 - 請透過編輯設定檔啟用
/etc/php/7.0/mods-available/pgsql.ini擴充功能。 組態檔應包含具有extension=php_pgsql.so文字的一行。 如果沒有顯示,請加入該文字並儲存檔案。 如果文字存在,但加上分號前置詞做為註解,請移除分號以取消文字註解。
macOS
- 下載 PHP 7.1.4 版本
- 安裝 PHP 並參考 PHP 手冊以便進一步設定
取得連線資訊
取得連接 適用於 PostgreSQL 的 Azure 資料庫 資料庫所需的連線資訊。 你需要完整合格的伺服器名稱和認證憑證。
- 登入 Azure 入口網站。
- 從 Azure 入口網站 左側功能表中,選取 [所有資源],然後搜尋您建立的伺服器(例如 mydemoserver)。
- 選取伺服器名稱。
- 從伺服器的 [概觀] 面板,記下 [伺服器名稱] 和 [伺服器管理員登入名稱]。 如果您忘記密碼,您也可以從此面板重設密碼。
連線及建立資料表
使用下列程序代碼,使用 CREATE TABLE SQL 語句連接及建立數據表,後面接著 INSERT INTO SQL 語句,以將數據列新增至數據表。
程式代碼呼叫方法 pg_connect() 可用來連線到 適用於 PostgreSQL 的 Azure 資料庫。 然後,它會呼叫 方法 - pg_query() - 數次執行數個命令,並 pg_last_error() 來檢查詳細數據是否每次發生錯誤。 然後,它會呼叫 方法 pg_close() 關閉連線。
將 $host、$database、$user 和 $password 參數替換為您的值。
<?php
// Initialize connection variables.
$host = "mydemoserver.postgres.database.azure.com";
$database = "mypgsqldb";
$user = "mylogin@mydemoserver";
$password = "<server_admin_password>";
// Initialize connection object.
$connection = pg_connect("host=$host dbname=$database user=$user password=$password")
or die("Failed to create connection to database: ". pg_last_error(). "<br/>");
print "Successfully created a connection to the database.<br/>";
// Drop the previous table of the same name if one exists.
$query = "DROP TABLE IF EXISTS inventory;";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). "<br/>");
print "Finished dropping table (if existed).<br/>";
// Create table.
$query = "CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). "<br/>");
print "Finished creating table.<br/>";
// Insert some data into the table.
$name = '\'banana\'';
$quantity = 150;
$query = "INSERT INTO inventory (name, quantity) VALUES ($name, $quantity);";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). "<br/>");
$name = '\'orange\'';
$quantity = 154;
$query = "INSERT INTO inventory (name, quantity) VALUES ($name, $quantity);";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). "<br/>");
$name = '\'apple\'';
$quantity = 100;
$query = "INSERT INTO inventory (name, quantity) VALUES ($name, $quantity);";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error()). "<br/>";
print "Inserted 3 rows of data.<br/>";
// Closing connection
pg_close($connection);
?>
讀取資料
使用下列程式碼搭配 SELECT SQL 陳述式來連線和讀取資料。
程式碼使用 pg_connect() 函式連接 適用於 PostgreSQL 的 Azure 資料庫。 接著它呼叫 pg_query() 來執行 SELECT 指令,並將結果保留在結果集中。 若發生錯誤,程式碼會呼叫 pg_last_error() 來檢查細節。 要讀取結果集,程式碼會以每列一次迴圈呼叫 pg_fetch_row() 來讀取。 列資料以陣列 $row取回,每個陣列位置每欄有一個資料值。 為了釋放結果集,程式碼呼叫 pg_free_result()。 完成後,程式碼呼叫 pg_close() 來關閉連線。
將 $host、$database、$user 和 $password 參數替換為您的值。
<?php
// Initialize connection variables.
$host = "mydemoserver.postgres.database.azure.com";
$database = "mypgsqldb";
$user = "mylogin@mydemoserver";
$password = "<server_admin_password>";
// Initialize connection object.
$connection = pg_connect("host=$host dbname=$database user=$user password=$password")
or die("Failed to create connection to database: ". pg_last_error(). "<br/>");
print "Successfully created a connection to the database. <br/>";
// Perform some SQL queries over the connection.
$query = "SELECT * from inventory";
$result_set = pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). "<br/>");
while ($row = pg_fetch_row($result_set))
{
print "Data row = ($row[0], $row[1], $row[2]). <br/>";
}
// Free result_set
pg_free_result($result_set);
// Closing connection
pg_close($connection);
?>
更新資料
使用下列程式碼搭配 UPDATE SQL 陳述式來連線和更新資料。
程式碼使用 pg_connect() 函式連接 適用於 PostgreSQL 的 Azure 資料庫。 接著它呼叫 pg_query() 來執行指令。 若發生錯誤,程式碼會呼叫 pg_last_error() 來檢查細節。 完成後,程式碼呼叫 pg_close() 來關閉連線。
將 $host、$database、$user 和 $password 參數替換為您的值。
<?php
// Initialize connection variables.
$host = "mydemoserver.postgres.database.azure.com";
$database = "mypgsqldb";
$user = "mylogin@mydemoserver";
$password = "<server_admin_password>";
// Initialize connection object.
$connection = pg_connect("host=$host dbname=$database user=$user password=$password")
or die("Failed to create connection to database: ". pg_last_error(). ".<br/>");
Print "Successfully created a connection to the database. <br/>";
// Modify some data in a table.
$new_quantity = 200;
$name = '\'banana\'';
$query = "UPDATE inventory SET quantity = $new_quantity WHERE name = $name;";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). ".<br/>");
print "Updated 1 row of data. </br>";
// Closing connection
pg_close($connection);
?>
刪除資料
請使用以下程式碼連接資料庫並使用 DELETE SQL 陳述式刪除資料。
程式碼使用 pg_connect() 函式連接 適用於 PostgreSQL 的 Azure 資料庫。 接著它呼叫 pg_query() 來執行指令。 若發生錯誤,程式碼會呼叫 pg_last_error() 來檢查細節。 完成後,程式碼呼叫 pg_close() 來關閉連線。
將 $host、$database、$user 和 $password 參數替換為您的值。
<?php
// Initialize connection variables.
$host = "mydemoserver.postgres.database.azure.com";
$database = "mypgsqldb";
$user = "mylogin@mydemoserver";
$password = "<server_admin_password>";
// Initialize connection object.
$connection = pg_connect("host=$host dbname=$database user=$user password=$password")
or die("Failed to create connection to database: ". pg_last_error(). ". </br>");
print "Successfully created a connection to the database. <br/>";
// Delete some data from a table.
$name = '\'orange\'';
$query = "DELETE FROM inventory WHERE name = $name;";
pg_query($connection, $query)
or die("Encountered an error when executing given sql statement: ". pg_last_error(). ". <br/>");
print "Deleted 1 row of data. <br/>";
// Closing connection
pg_close($connection);
?>
清理資源
若要清除本快速入門期間使用的所有資源,請使用下列命令刪除資源群組:
az group delete \
--name $AZ_RESOURCE_GROUP \
--yes
相關內容
- 使用 Azure 入口網站管理適用於 PostgreSQL 的 Azure 資料庫。
- 快速入門:使用 Python 從適用於 PostgreSQL 的 Azure 資料庫連線和查詢資料。
- 快速入門:使用 Java 從適用於 PostgreSQL 的 Azure 資料庫連線和查詢資料。
- 快速入門:使用 .NET (C#) 從適用於 PostgreSQL 的 Azure 資料庫連線和查詢資料。
- 快速入門:使用 Go 語言從適用於 PostgreSQL 的 Azure 資料庫連線和查詢資料。
- 快速入門:使用 Azure CLI 從適用於 PostgreSQL 的 Azure 資料庫連線和查詢資料。
- 快速入門:從 Power BI 中的適用於 PostgreSQL 的 Azure 資料庫匯入資料。