php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33809 pg_fetch_result: table alias produces invalid column result error
Submitted: 2005-07-21 19:22 UTC Modified: 2005-07-29 01:00 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: torpedo51 at yahoo dot com Assigned:
Status: No Feedback Package: PostgreSQL related
PHP Version: 5.0.3 OS: Linux 2.6.11
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2005-07-21 19:22 UTC] torpedo51 at yahoo dot com
Description:
------------
Using table aliases when retrieving a column value (for the purpose of resolving column name ambiguity) is producing an "invalid column name" error.  

This produces the error:
$sql = "select t1.c1, t2.c1 from..."
$intValue1 = pg_fetch_result($rs, $intRow, 't1.c1');
$intValue2 = pg_fetch_result($rs, $intRow, 't2.c1');

This is ambiguous but does not produce an error:
$sql = "select t1.c1, t2.c1 from..."
$intValue1 = pg_fetch_result($rs, $intRow, 'A');
$intValue2 = pg_fetch_result($rs, $intRow, 'B');

This is valid:
$sql = "select t1.c1 as A, t2.c1 as B from..."
$intValue1 = pg_fetch_result($rs, $intRow, 'A');
$intValue2 = pg_fetch_result($rs, $intRow, 'B');




PostGres: 8.0.3

php.ini diff:
ShortTags = Off


Reproduce code:
---------------
$sql = "select t1.c1, t2.c1 from table1 t1, table2 t2";
$rs = pg_query($sql);
$intValue = pg_fetch_result($rs, $intRow, 't1.c1');



Expected result:
----------------
I expect to be able to retrieve the columns using the explicit name that includes the table alias.

Previous versions did not produce this error.  I also ran this same test using mySql (mysql_fetch) which does not produce the error.

I also tested this which does not produce the error: 
$row = pg_fetch_assoc($rs);
$intValue1 = $row['t1.c1']
$intValue2 = $row['t2.c1']



Actual result:
--------------
[client 127.0.0.2] PHP Warning: pg_fetch_result() [function.pg-fetch-result]: Bad column offset specified in /home/spr/Documents/web/spr/spr4/web/10dev/modules/datasvcs.php on line 210

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-21 19:25 UTC] torpedo51 at yahoo dot com
Sorry - wrong again.  Third time's a charm:

This is ambiguous but does not produce an error:
$sql = "select t1.c1, t2.c1 from..."
$intValue1 = pg_fetch_result($rs, $intRow, 'c1');
$intValue2 = pg_fetch_result($rs, $intRow, 'c1');
 [2005-07-21 22:36 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-07-29 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2006-09-22 12:30 UTC] alan8 at maths dot topology dot org
A successful work-around for this bug is to use quotes.

So maybe the bug should just be documented as a feature. The following work-around code works fine.

$res = pg_query(...);
$colname = pg_field_name($res, $j);
pg_fetch_result($res, $i, "\"$colname\"");

But if you use this:

pg_fetch_result($res, $i, $colname);

The functions fails as reported by torpedo51 when there are upper-case characters in the column name.

PHP version = 5.1.4.
pqsl version = 8.1.4.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Thu Apr 17 03:01:55 2014 UTC