Skip to content

pg_fetch_result Shows Incorrect ArgumentCountError Message when Called With 1 Argument #17158

Closed

Description

@it-all

Description

The following code:

<?php

// use any PgSql\Result $pgResult below
pg_fetch_result($pgResult);

Resulted in this output:

PHP Fatal error:  Uncaught ArgumentCountError: pg_fetch_result() expects exactly 3 arguments, 1 given

But I expected this output instead:

PHP Fatal error:  Uncaught ArgumentCountError: pg_fetch_result() expects 2 or 3 arguments, 1 given

[The documentation](https://www.php.net/manual/en/function.pg-fetch-result.php) is unclear, as it says the 2nd argument (row), may be omitted, but does not say the 3rd argument (field) may be omitted.
The code pg_fetch_result($pgResult, null); does work and returns the first field of the first row. 
I think the documentation needs to be updated as well as the error message.

PHP Version

PHP 8.3.14

Operating System

Ubuntu 24.04

Activity

  1. ndossche commented on Dec 14, 2024

    @ndossche
    Member

    This is because the way the argument parsing is implemented is strange:

    php-src/ext/pgsql/pgsql.c

    Lines 1884 to 1907 in c015242

    if (ZEND_NUM_ARGS() == 2) {
    if (zend_string_equals_literal(EX(func)->common.function_name, "pg_fetch_result")) {
    zend_error(E_DEPRECATED, "Calling pg_fetch_result() with 2 arguments is deprecated, "
    "use the 3-parameter signature with a null $row parameter instead");
    if (UNEXPECTED(EG(exception))) {
    RETURN_THROWS();
    }
    }
    ZEND_PARSE_PARAMETERS_START(2, 2)
    Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
    Z_PARAM_STR_OR_LONG(field_name, field_offset)
    ZEND_PARSE_PARAMETERS_END();
    } else {
    ZEND_PARSE_PARAMETERS_START(3, 3)
    Z_PARAM_OBJECT_OF_CLASS(result, pgsql_result_ce)
    if (zend_string_equals_literal(EG(current_execute_data)->func->common.function_name, "pg_result")) {
    Z_PARAM_LONG(row)
    } else {
    Z_PARAM_LONG_OR_NULL(row, row_is_null)
    }
    Z_PARAM_STR_OR_LONG(field_name, field_offset)
    ZEND_PARSE_PARAMETERS_END();
    }

    This can be solved by adding another branch that checks whether not enough arguments for both cases are passed.

  2. self-assigned this
    on Dec 14, 2024
  3. added 3 commits that reference this issue on Dec 14, 2024
    ea05086
    388f63c
    e562b8c
  4. added a commit that references this issue on Dec 14, 2024
    57e9429
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions