Doc #72058 [Ver->Csd]: pg_field_num Error with Quoted Aliases

[email protected] Thu, 11 Nov 2021 11:33:56 +0000
Newsgroups php.doc.bugs
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=72058&edit=1

 ID:                 72058
 Updated by:         [email protected]
 Reported by:        klaxian at gmail dot com
 Summary:            pg_field_num Error with Quoted Aliases
-Status:             Verified
+Status:             Closed
 Type:               Documentation Problem
 Package:            PostgreSQL related
 Operating System:   Debian Linux
 PHP Version:        5.5.34
 Block user comment: N
 Private report:     N

 New Comment:

Automatic comment on behalf of cmb69
Revision: https://github.com/php/doc-en/commit/6fcf14255c6027d7adf48a5f71f6a1c9a12018b2
Log: Fix #72058: pg_field_num Error with Quoted Aliases


Previous Comments:
------------------------------------------------------------------------
[2021-08-09 14:03:47] [email protected]

pg_field_num() is a thin wrapper of PQfnumber()[1], and this is
how this function behaves:

| The given name is treated like an identifier in an SQL command,
| that is, it is downcased unless double-quoted.

In other words, this is not a bug, but rather should be documented
in the PHP manual.

[1] <https://www.postgresql.org/docs/10/libpq-exec.html#LIBPQ-PQFNUMBER>

------------------------------------------------------------------------
[2020-03-04 12:24:38] tobias dot john at foo dot ag

This ist still present in PHP 7.4.1.

------------------------------------------------------------------------
[2020-03-03 11:53:47] marcel at wieland dot koeln

Same with pg_fetch_result().

------------------------------------------------------------------------
[2016-04-19 12:56:19] klaxian at gmail dot com

Description:
------------
When selecting data from a PostgreSQL database using quoted aliases for column names (to preserve case, for example), pg_field_num() seems to expect its input to also contain double quotes.  However, returned values from functions like pg_field_name() and pg_fetch_assoc() do not contain double quotes.  PostgreSQL's own client also does not include double quotes in the resulting column names.  Even if the behavior of pg_field_num is intentional, there does not seem to be a way to determine if field aliases have been quoted using the result resource only.

I suggest fixing pg_field_num() so that it is consistent with the other PostgreSQL functions.  I would expect the function to accept the actual field name, not encapsulated in double quotes.

Test script:
---------------
$result = pg_query('SELECT \'test\' AS "TeSt"');
var_dump(pg_field_name($result, 0)); // returns TeSt
var_dump(pg_field_num($result, 'TeSt')); // returns -1 (error)
var_dump(pg_field_num($result, '"TeSt"')); // returns 0 (first column)


Expected result:
----------------
I expect pg_field_num($result, 'TeSt') to return 0 instead of an error.

Actual result:
--------------
-1


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72058&edit=1