查詢

pg_get_result()函式—用法及示例

「 獲取一個非阻塞的 PostgreSQL 查詢的結果 」


函式名稱:pg_get_result()

函式描述:pg_get_result()函式用於獲取一個非阻塞的 PostgreSQL 查詢的結果。

適用版本:該函式適用於 PHP 4 >= 4.2.0 和 PHP 5 版本。

語法:resource pg_get_result ( resource $connection )

引數:

  • $connection:一個有效的 PostgreSQL 連線資源。

返回值:返回一個結果集資源,該資源可以用於獲取查詢的結果。

示例:

// 連線到 PostgreSQL 資料庫
$conn = pg_connect("host=localhost dbname=mydb user=myuser password=mypassword");

// 執行一個查詢
$query = "SELECT * FROM mytable";
$result = pg_query($conn, $query);

// 檢查查詢是否成功
if (!$result) {
    echo "查詢執行失敗!";
    exit;
}

// 獲取查詢的結果
$result_set = pg_get_result($conn);

// 檢查結果集是否為空
if (pg_num_rows($result_set) == 0) {
    echo "查詢結果為空!";
    exit;
}

// 遍歷結果集並輸出資料
while ($row = pg_fetch_assoc($result_set)) {
    echo "ID:" . $row['id'] . ",名稱:" . $row['name'] . "<br>";
}

// 釋放結果集資源
pg_free_result($result_set);

// 關閉資料庫連線
pg_close($conn);

在上面的示例中,我們首先使用pg_connect()函式連線到 PostgreSQL 資料庫。然後,我們執行一個查詢,並使用pg_get_result()函式獲取查詢的結果。接下來,我們檢查結果集是否為空,並使用pg_fetch_assoc()函式遍歷結果集並輸出資料。最後,我們使用pg_free_result()函式釋放結果集資源,並使用pg_close()函式關閉資料庫連線。

請注意,pg_get_result()函式只適用於非阻塞的查詢,如果查詢是阻塞的,則該函式將返回 FALSE。

補充糾錯
上一個函式: pg_host()函式
下一個函式: pg_get_pid()函式
熱門PHP函式
分享連結