天天看點

php pg_fetch_row,PHP: pg_fetch_result - Manual

In order to use upper case in pg_fetch_result column names, it is apparently necessary to include explicit quotation marks.

Thus when I do this sort of thing:

$res = pg_query(...);

$ncols = pg_num_fields($res);

for ($j = 0; $j < $ncols; ++$j) {

$colname[$j] = pg_field_name($res, $j);

$name = htmlspecialchars($colname[$j]);

print("Column $j name = \"$name\"\n");

$value = htmlspecialchars(pg_fetch_result($res, 0, $colname[$j]));

print("Column \"{$colname[$j]}\" value = \"$value\"\n");

}

I get this sort of thing:

[....]

Warning: pg_fetch_result() [function.pg-fetch-result]: Bad column offset specified in /.../view.php on line 247

Column 8 name = "VEC index"

Column "VEC index" value = ""

But if I change the $value line to this:

$value = htmlspecialchars(pg_fetch_result($res, 0, "\"$colname[$j]\""));

I get this:

[...]

Column 8 name = "VEC index"

Column "VEC index" value[0] = "47"

In my opinion, pg_fetch_result(...) should use the quotes already. In other words, this may be a bug in the PHP postgres library. It does not seem to be a documented feature of pg_fetch_result() although the postgresql manual documents it under "SQL syntax", "Lexical structure".

PHP version 5.1.4.

psql version 8.1.4.