spaceman-8080f-20040129@ausgehaucht.sensenmann.at wrote:[color=blue]
> Hi.
>
> The manual for pg_fetch_result() reads:
>
>| [..] All forms of integer types are returned as integer values. All
>| forms of float, and real types are returned as float values. Boolean
>| is returned as "t" or "f". All other types, including arrays are
>| returned as strings formatted in the same default PostgreSQL manner
>| that you would see in the psql program.
>
> This is not what happens when I call pg_fetch_result():
>
> pg_query($conn, "create table foo (id int)");
> pg_query($conn, "insert into foo values (42)");
> $result = pg_query($conn, "select * from foo");
> $id = pg_fetch_result($result, 0, 0);
> var_dump($id);
>
> // prints: string(2) "42"
>
> I get the same behaviour for other types of integers, and for floats.
> Booleans are returned as "t" and "f" (quite unfortunately, as both
> strings evaluate to true in PHP), but that's at least what the manual
> says should happen.
>
> Is there any way to get PHP to do the casting for me, or do I really
> have to convert all the values manually? I'm currently using PHP 4.3.1
> with PostgreSQL 7.3.2.[/color]
You are right - it doesn't work as documented. PHP (through 4.3.3 at least)
has no code to convert results to specific types, but returns everything as
strings (not counting the new "experimental" functions). Feel free to
submit a bug report, but I would make it a documentation bug, since I think
that PHP should not try to do the conversion. It would be a lot less
efficient than you converting it in your PHP script. The PostgreSQL
interface library libpq hands everthing back to PHP as strings anyway, so
PHP would have to find the types (by OID, then ask the database to map that
to a type name), then do the conversion. Since your script 'knows' what
type is expected, it's got to be more efficient to do any needed conversion
in your PHP script.