PHP 8.4.2 Released!

pg_result_status

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

pg_result_status 获得查询结果的状态

说明

pg_result_status(PgSql\Result $result, int $mode = PGSQL_STATUS_LONG): string|int

pg_result_status() 返回 PgSql\Result 实例的状态,或者与 result 相关的 PostgreSQL 命令完成标记。

参数

result

PgSql\Result 实例,由 pg_query()pg_query_params() 或者 pg_execute()(等)返回。

mode

要么指定 PGSQL_STATUS_LONG 返回 result 的数字状态,要么指定 PGSQL_STATUS_STRING 返回 result 的命令标记。如果未指定,默认是 PGSQL_STATUS_LONG

返回值

如果指定 PGSQL_STATUS_LONG,可能返回的值是 PGSQL_EMPTY_QUERYPGSQL_COMMAND_OKPGSQL_TUPLES_OKPGSQL_COPY_OUTPGSQL_COPY_INPGSQL_BAD_RESPONSEPGSQL_NONFATAL_ERRORPGSQL_FATAL_ERROR。否则,返回包含 PostgreSQL 命令标记的 string

更新日志

版本 说明
8.1.0 现在 result 参数接受 PgSql\Result 实例,之前接受 resource

示例

示例 #1 pg_result_status() 示例

<?php

// Connect to the database
$conn = pg_pconnect("dbname=publisher");

// Execute a COPY
$result = pg_query($conn, "COPY authors FROM STDIN;");

// Get the result status
$status = pg_result_status($result);

// Determine status
if ($status == PGSQL_COPY_IN)
echo
"Copy began.";
else
echo
"Copy failed.";

?>

以上示例会输出:

Copy began.

参见

添加备注

用户贡献的备注 4 notes

up
2
romix at gmx dot de
22 years ago
0 = PGSQL_EMPTY_QUERY
1 = PGSQL_COMMAND_OK
2 = PGSQL_TUPLES_OK
3 = PGSQL_COPY_TO
4 = PGSQL_COPY_FROM
5 = PGSQL_BAD_RESPONSE
6 = PGSQL_NONFATAL_ERROR
7 = PGSQL_FATAL_ERROR
up
2
Anonymous
10 years ago
This function is useful it may not return a full non fatal error but you want to see if the query completes anyway.

example

<?PHP
//connect to pg database
// create PGSQL

$PGSTAT = pg_result_status($PGSQL);//defaults to long

//And run it through a check for example

if(($PGSTAT == 1) || ($PGSTAT == 2)){
// for query results;
}else if(($PGSTAT == 3) || ($PGSTAT == 4)){
// for copy results;
}
if((
$PGSTAT != 0) && ($PGSTAT != 5) && ($PGSTAT != 6) && ($PGSTAT != 7)){
// your code;
}else{
// $ERRORS codes;
}
// This is worked the best for me so far I tried using
// The command tag and I seem to still get an numeric result
//example
//$PGSTAT = pg_result_status($PGSQL, PGSQL_COMMAND_OK);
// got something like tuples ok so I recommend just use default long
?>
or use what ever condition check you feel fit
up
-1
Meej Vaj
19 years ago
If on "accident" your program passes pg_result_status a variable that is NOT a actual pg resource, it will return an empty string.
(This is important to know if you want to make sure a pg resource is "good" before attempting some operation on it).
up
-1
dean at deansas dot org
11 years ago
The postgres docs say that PHP should never be able to return PGRES_NONFATAL_ERROR

"A result of status PGRES_NONFATAL_ERROR will never be returned directly by PQexec or other query execution functions" http://www.postgresql.org/docs/current/static/libpq-exec.html

Therefore you cannot use this function to check if this query produced a notice.
To Top