查詢

pg_unescape_bytea()函式—用法及示例

「 將 PostgreSQL 資料庫中 bytea 型別的資料轉換為二進位制字串 」


函式名稱:pg_unescape_bytea()

適用版本:PHP 5 >= 5.4.0, PHP 7, PHP 8

函式描述:pg_unescape_bytea() 函式用於將 PostgreSQL 資料庫中 bytea 型別的資料轉換為二進位制字串。

用法:

string pg_unescape_bytea ( resource $connection , string $data ) 

引數:

  • connection:一個有效的 PostgreSQL 連線資源。
  • data:要解碼的 bytea 型別資料。

返回值:

  • 解碼後的二進位制字串,如果解碼失敗則返回 FALSE。

示例:

// 建立 PostgreSQL 連線
$conn = pg_connect("host=localhost dbname=mydb user=myuser password=mypassword");

// 查詢資料庫獲取 bytea 型別資料
$result = pg_query($conn, "SELECT image_data FROM images WHERE id = 1");
$data = pg_fetch_result($result, 0, 0);

// 解碼 bytea 資料為二進位制字串
$binaryData = pg_unescape_bytea($conn, $data);

// 將二進位制字串儲存為檔案
file_put_contents('image.jpg', $binaryData);

// 關閉連線
pg_close($conn);

在上述示例中,我們首先使用 pg_connect() 函式建立與 PostgreSQL 資料庫的連線。然後,我們執行一個查詢語句獲取資料庫中的 bytea 型別資料。接下來,我們使用 pg_unescape_bytea() 函式將獲取的 bytea 資料解碼為二進位制字串。最後,我們使用 file_put_contents() 函式將二進位制字串儲存為一個名為 image.jpg 的檔案。最後,我們使用 pg_close() 函式關閉與資料庫的連線。

請注意,pg_unescape_bytea() 函式只能在與 PostgreSQL 資料庫連線的情況下使用,因此在使用該函式之前,必須先建立一個有效的連線。

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