PHP 日本會議 2024

pg_result_error

(PHP 4 >= 4.2.0,PHP 5,PHP 7,PHP 8)

pg_result_error 取得與結果相關聯的錯誤訊息

描述

pg_result_error(PgSql\Result $result): string|false

pg_result_error() 會傳回與 result 實例相關聯的任何錯誤訊息。因此,使用者比使用 pg_last_error() 更能取得正確的錯誤訊息。

函式 pg_result_error_field() 可以提供比 pg_result_error() 更詳細的結果錯誤資訊。

因為如果查詢失敗,pg_query() 會傳回 false,所以您必須使用 pg_send_query()pg_get_result() 來取得結果處理常式。

參數

result

pg_query()pg_query_params()pg_execute()(以及其他函式)傳回的 PgSql\Result 實例。

傳回值

傳回 string。如果沒有錯誤,則傳回空字串。如果 result 參數有關聯的錯誤,則傳回 false

變更記錄

版本 描述
8.1.0 result 參數現在預期是 PgSql\Result 實例;先前,預期是 資源

範例

範例 1 pg_result_error() 範例

<?php
$dbconn
= pg_connect("dbname=publisher") or die("無法連線");

if (!
pg_connection_busy($dbconn)) {
pg_send_query($dbconn, "select * from doesnotexist;");
}

$res1 = pg_get_result($dbconn);
echo
pg_result_error($res1);
?>

參見

新增註解

使用者貢獻的註解 4 則註解

spamuser at test dot com
13 年前
pg_result_error 無法與預先處理的陳述式搭配使用。
VLroyrenn
5 年前
因此,考量到 pg_query 和 pg_query_params 在發生錯誤時不會傳回結果,而且這會是非常方便的功能,我最終編寫了自己的函式來封裝 pg_send_* 函式,使其像前述的兩個函式 <em>應該</em> 一樣運作。它們的程式碼不多,註解主要是來自 libPQ 和 PHP 文件的參考,以解釋其行為。

<?php

class PostgresConnectionError extends Exception {
public function
__construct($last_error) {
parent::__construct($message);
}
}

function
pg_send_query_sync($connection, string $query) {
// 如果該連線已經有查詢正在執行,此函式將無法運作,因為結果可能會混淆。
assert(pg_get_result($connection) === false);

$dispatch_ok = pg_send_query($connection, $query);

// 唯一無法回傳任何東西的情況是,當連線無法派送初始查詢時。
if (!$dispatch_ok) throw new PostgresConnectionError(pg_​last_​error($connection));

// 根據 libPQ 文件:「必須重複呼叫 PQgetResult,直到它返回空指標,表示命令已完成。」

// 就我們嘗試模擬的 pg_query/PQExec 而言:
// 「但請注意,返回的 PGresult 結構僅描述從字串執行的最後一個命令的結果。」
// 「如果其中一個命令失敗,則字串的處理將停止,並且返回的 PGresult 描述錯誤狀況。」

while ($result = pg_get_result($connection)) {
// 排空連線上的所有結果,並僅返回最後一個。
if ($last_result) pg_free_result($last_result);
$last_result = $result;
}

assert(is_resource($result) && get_resource_type($result) === "pgsql result");
return
$last_result;
}

function
pg_send_query_params_sync($connection, string $query, array $params) {
// 如果該連線已經有查詢正在執行,此函式將無法運作,因為結果可能會混淆。
assert(pg_get_result($connection) === false);

$dispatch_ok = pg_send_query_params($connection, $query, $params);

// 唯一無法回傳任何東西的情況是,當連線無法派送初始查詢時。
if (!$dispatch_ok) throw new PostgresConnectionError(pg_​last_​error($connection));

// 根據 libPQ 文件:「必須重複呼叫 PQgetResult,直到它返回空指標,表示命令已完成。」

// 就我們嘗試模擬的 pg_query_params/PQExecParams 而言:
// 「與 PQexec 不同,PQexecParams 在給定字串中最多允許一個 SQL 命令。(其中可以有分號,但不能有多個非空命令。)」

while ($result = pg_get_result($connection)) {
// 排空連線上的所有結果,儘管應該只有一個。
if ($last_result) pg_free_result($last_result);
$last_result = $result;
}

assert(is_resource($result) && get_resource_type($result) === "pgsql result");
return
$last_result;
}
axon dot wp at wp dot pl
18 年前
您可以使用 pg_set_error_verbosity() 從 pg_last_error() 檢索 SQLSTATUS。
Anonymous
20 年前
因為如果查詢失敗,pg_query() 會返回 FALSE,您必須使用 pg_send_query() 和 pg_get_result() 來取得結果處理。

PostgreSQL 7.4 引入了一個名為 PQresultErrorField() 的新函式,可用於從查詢取得 SQLSTATE 程式碼,這比從 pg_result_error() 返回的錯誤字串更有用。
https://postgresql.dev.org.tw/docs/7.4/static/libpq-exec.html
https://postgresql.dev.org.tw/docs/7.4/static/errcodes-appendix.html

這是一個將 pg_result_error_field() 函式新增到 PHP 的修補程式
http://collapsed.net/patches/php-4.3.5RC3-pg_result_error_field.diff
(要套用修補程式,請 cd 到您的 php-4.3.5RC 目錄並輸入:patch -p1 </path/to/php-4.3.5RC3-pg_result_error_field.diff)

程式碼範例
<?php
if(!($db = pg_connect("user=foo password=bar dbname=foobar")))
die(
"pg_connect");

if(!
pg_send_query($db, "SELECT foo FROM bar"))
die(
"pg_send_query");

if(!(
$result = pg_get_result($db)))
die(
"pg_get_result");

echo(
pg_result_error($result) . "<br />\n");

/* 只有在您已修補 php 的情況下才可用 */
if(function_exists("pg_result_error_field"))
{
$fieldcode = array(
"PGSQL_DIAG_SEVERITY", "PGSQL_DIAG_SQLSTATE",
"PGSQL_DIAG_MESSAGE_PRIMARY", "PGSQL_DIAG_MESSAGE_DETAIL",
"PGSQL_DIAG_MESSAGE_HINT", "PGSQL_DIAG_STATEMENT_POSITION",
"PGSQL_DIAG_CONTEXT", "PGSQL_DIAG_SOURCE_FILE",
"PGSQL_DIAG_SOURCE_LINE", "PGSQL_DIAG_SOURCE_FUNCTION");

foreach(
$fieldcode as $fcode)
{
printf("%s: %s<br />\n",
$fcode,
pg_result_error_field($result, constant($fcode)));
}

pg_free_result($result);
}
?>

<?php pg_result_error_field($result, PGSQL_DIAG_SQLSTATE); ?>
返回 SQLSTATE 程式碼。
To Top