Connecting Tech Pros Worldwide Forums | Help | Site Map

pg_fetch_result() always returns strings

Stefan Weiss
Guest
 
Posts: n/a
#1: Jul 17 '05
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.

TIA,
stefan

ljb
Guest
 
Posts: n/a
#2: Jul 17 '05

re: pg_fetch_result() always returns strings


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.
martin
Guest
 
Posts: n/a
#3: Jul 17 '05

re: pg_fetch_result() always returns strings


Stefan Weiss <spaceman-8080f-20040129@ausgehaucht.sensenmann.at> wrote in message news:<3665724.bxPTZExDLK@weyoun.foo.at>...[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.
>
> TIA,
> stefan
>[/color]

The way to get PHP to do the casting for you is to ignore type,
and let type be inferred from the operation being performed.
Stefan Weiss
Guest
 
Posts: n/a
#4: Jul 17 '05

re: pg_fetch_result() always returns strings


ljb <ljb220@mindspring.nospam.com> wrote:
[color=blue]
> 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.[/color]

I don't agree; at the very least I would like an option to have the
fields converted automatically. That way I would always get a float
out of a float field, regardless of locale settings: if PostgreSQL's
LC_NUMERIC was set to de_DE, the decimal seperator would be a comma,
and PHP would interpret the string "4,321" as 4 (integer).

[@martin: This is why I can't just let PHP do the casting; especially
not in a financial application. ]
[color=blue]
> 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.[/color]

I usually have custom functions that handle all the db access. These
functions don't know in advance what types they are dealing with, so
they would have to call pg_field_type() once for each returned column,
and then convert as needed. That most certainly is not as efficient as
PHP getting the column types while it's talking to the db backend (OIDs
have nothing to do with this, btw).

And even if I knew which types are going to be returned, I would have
to do something like this after each query:

$values = some_query(); // returns 2dim array of rows/fields

for ($i = 0; $i < count($values); ++$i) {

// numfield should be integer value
if ($values[$i]["numfield"] !== null) {
$values[$i]["numfield"] = (int) $values[$i]["numfield"];
}

// boolfield is a boolean field
if ($values[$i]["boolfield"] !== null) {
$values[$i]["boolfield"] = $values[$i]["boolfield"] != "f";
}

// fltfield is a float field
if ($values[$i]["fltfield"] !== null) {
$values[$i]["fltfield"] = parse_float_rep($values[$i]["fltfield"]);
}

// ... etc ...

}

Not exactly my idea of efficience.

Oh well, we can always upgrade the hardware :-/.


cheers,
stefan
Closed Thread