How to retrieve the result status in a PostgreSQL database?

When using the pg database, you can obtain the result status using the following methods:

  1. Execute SQL query statements using the pg_query() function and store the results in a variable. For example:
$query = pg_query($dbconn, "SELECT * FROM table_name");
  1. PGSQL_EMPTY_QUERY: The query is empty
  2. PGSQL_COMMAND_OK: Query was successful.
  3. PGSQL_TUPLES_OK: The query returns a result set.
  4. PGSQL_COPY_OUT: Query in COPY OUT mode.
  5. PGSQL_COPY_IN: operated in COPY IN mode.
  6. PGSQL_BAD_RESPONSE: Abnormal query result.

For example:

$status = pg_result_status($query);
  1. Different actions can be taken based on the result status. For example, if the status is PGSQL_TUPLES_OK, it means that the query has returned a result set, and you can use functions like pg_fetch_assoc() or pg_fetch_row() to fetch data from each row.
if ($status === PGSQL_TUPLES_OK) {
    while ($row = pg_fetch_assoc($query)) {
        // 处理每一行的数据
    }
}

The above is a method for operating pg databases in PHP using the pg library. Similar methods for obtaining result status can also be found when operating pg databases with other programming languages.

bannerAds