|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #72058 pg_field_num Error with Quoted Aliases
Submitted: 2016-04-19 12:56 UTC Modified: 2021-08-09 14:03 UTC
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: klaxian at gmail dot com Assigned:
Status: Verified Package: PostgreSQL related
PHP Version: 5.5.34 OS: Debian Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: klaxian at gmail dot com
New email:
PHP Version: OS:


 [2016-04-19 12:56 UTC] klaxian at gmail dot com
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:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-03 11:53 UTC] marcel at wieland dot koeln
Same with pg_fetch_result().
 [2020-03-04 12:24 UTC] tobias dot john at foo dot ag
This ist still present in PHP 7.4.1.
 [2021-08-09 14:03 UTC]
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-08-09 14:03 UTC]
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] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Sep 17 11:03:36 2021 UTC