Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

nrao wwwxxx, xxxxxdup, procmail change subject header, wwwXxx not20, Wwwxxx.doks sas, linux raid resync after reboot, bind-address mysql multiple, sanibleone xxxx, ftp://192.168.100.100/, www.xxxcon

Links

XODOX
Impressum

#1: PHP and PostgreSQL boolean data type

Posted on 2010-02-10 13:04:23 by Thom Brown

Hi,

A long-standing problem we've had with PostgreSQL queries in PHP is
that the returned data for boolean columns is the string 'f' instead
of the native boolean value of false.

An obvious example of this would be for a table with users and their
boolean registered status:

Select user, registered From users;

Then getting a row from the result would reveal: array('user' =>
'thomb', registered => 'f');

Another problem is with arrays, where they are difficult to parse as
they also come through as plain strings with no binary alternative.

Is this a limitation of libpq or a flawed implementation in the php
library? And if this is just the case for backwards-compatibility, is
there a way to switch it to a more sensible PHP data type?

Thanks

Thom

--
Sent via pgsql-php mailing list (pgsql-php@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Report this message

#2: Re: PHP and PostgreSQL boolean data type

Posted on 2010-02-10 13:13:22 by Tommy Gildseth

Thom Brown wrote:
> Is this a limitation of libpq or a flawed implementation in the php
> library? And if this is just the case for backwards-compatibility, is
> there a way to switch it to a more sensible PHP data type?

Using PDO(http://no.php.net/pdo) will at least give you native values
for true/false. Arrays, I don't know, since I don't use them.

--
Tommy Gildseth

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

Report this message

#3: Re: PHP and PostgreSQL boolean data type

Posted on 2010-02-10 22:56:36 by dmagick

Thom Brown wrote:
> Hi,
>
> A long-standing problem we've had with PostgreSQL queries in PHP is
> that the returned data for boolean columns is the string 'f' instead
> of the native boolean value of false.
>
> An obvious example of this would be for a table with users and their
> boolean registered status:
>
> Select user, registered From users;
>
> Then getting a row from the result would reveal: array('user' =>
> 'thomb', registered => 'f');

That's how postgres stores them, php doesn't understand the field is a
boolean.

# create table a(a int, b boolean);
# insert into a(a, b) values (1, true);
# insert into a(a, b) values (2, false);
# SELECT * from a;
a | b
---+---
1 | t
2 | f
(2 rows)

Also while not in the "official" docs, it is a note from 2002:

http://www.php.net/manual/en/ref.pgsql.php#18749

and

http://www.php.net/manual/en/function.pg-fetch-array.php says

Each value in the array is represented as a string. Database NULL
values are returned as NULL.


> Another problem is with arrays, where they are difficult to parse as
> they also come through as plain strings with no binary alternative.

Haven't played with postgres arrays so can't say either way - but same
as above, php just fetches the data.

There's an example that might help you -
http://www.php.net/manual/en/ref.pgsql.php#89841

--
Postgresql & php tutorials
http://www.designmagick.com/


--
Sent via pgsql-php mailing list (pgsql-php@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Report this message

#4: Re: PHP and PostgreSQL boolean data type

Posted on 2010-02-12 10:13:54 by Jasen Betts

On 2010-02-10, Thom Brown <thombrown@gmail.com> wrote:
> Hi,
>
> A long-standing problem we've had with PostgreSQL queries in PHP is
> that the returned data for boolean columns is the string 'f' instead
> of the native boolean value of false.
>
> An obvious example of this would be for a table with users and their
> boolean registered status:
>
> Select user, registered From users;
>
> Then getting a row from the result would reveal: array('user' =>
> 'thomb', registered => 'f');
>
> Another problem is with arrays, where they are difficult to parse as
> they also come through as plain strings with no binary alternative.
>
> Is this a limitation of libpq or a flawed implementation in the php
> library? And if this is just the case for backwards-compatibility, is
> there a way to switch it to a more sensible PHP data type?

cast to integer when selecting and to boolean when writing?


--
Sent via pgsql-php mailing list (pgsql-php@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Report this message