pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PHP and PostgreSQL boolean data type
8+ messages / 6 participants
[nested] [flat]

* PHP and PostgreSQL boolean data type
@ 2010-02-10 12:04  Thom Brown <thombrown@gmail.com>
  0 siblings, 6 replies; 8+ messages in thread

From: Thom Brown @ 2010-02-10 12:04 UTC (permalink / raw)
  To: pgsql-php@postgresql.org, PGSQL Mailing List <pgsql-general@postgresql.org>

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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-10 12:11  A. Kretschmer <andreas.kretschmer@schollglas.com>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 1 reply; 8+ messages in thread

From: A. Kretschmer @ 2010-02-10 12:11 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

In response to 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.

http://andreas.scherbaum.la/blog/archives/302-BOOLEAN-datatype-with-PHP-compatible-output.html

Regards, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-10 12:13  Tommy Gildseth <tommy.gildseth@usit.uio.no>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 0 replies; 8+ messages in thread

From: Tommy Gildseth @ 2010-02-10 12:13 UTC (permalink / raw)
  To: Thom Brown <thombrown@gmail.com>; +Cc: pgsql-php@postgresql.org, PGSQL Mailing List <pgsql-general@postgresql.org>

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



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-10 12:44  Thom Brown <thombrown@gmail.com>
  parent: A. Kretschmer <andreas.kretschmer@schollglas.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Thom Brown @ 2010-02-10 12:44 UTC (permalink / raw)
  To: A. Kretschmer <andreas.kretschmer@schollglas.com>; +Cc: pgsql-general@postgresql.org

On 10 February 2010 12:11, A. Kretschmer
<andreas.kretschmer@schollglas.com> wrote:
> In response to 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.
>
> http://andreas.scherbaum.la/blog/archives/302-BOOLEAN-datatype-with-PHP-compatible-output.html
>

Thanks guys.  I can see that this is specifically a PHP issue then.
It seems like an extreme workaround though.  I'd rather see the PHP
library updated in a way that would somehow not break existing code
which checked for an 'f'.  Not quite sure what the solution would be.

Thanks

Thom



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-10 14:15  Torsten Zühlsdorff <foo@meisterderspiele.de>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 0 replies; 8+ messages in thread

From: Torsten Zühlsdorff @ 2010-02-10 14:15 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

Thom Brown schrieb:

> 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.

This problem is solved since nearly 5 years with PDO. You can use an 
abstraction like DDDBL (see my signature) if you want to save time while 
using PDO.

Greetings from Germany,
Torsten

-- 
http://www.dddbl.de - ein Datenbank-Layer, der die Arbeit mit 8 
verschiedenen Datenbanksystemen abstrahiert,
Queries von Applikationen trennt und automatisch die Query-Ergebnisse 
auswerten kann.



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: [PHP] PHP and PostgreSQL boolean data type
@ 2010-02-10 21:56  Chris <dmagick@gmail.com>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 0 replies; 8+ messages in thread

From: Chris @ 2010-02-10 21:56 UTC (permalink / raw)
  To: Thom Brown <thombrown@gmail.com>; +Cc: pgsql-php@postgresql.org, PGSQL Mailing List <pgsql-general@postgresql.org>

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/




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-11 07:42  Torsten Zühlsdorff <foo@meisterderspiele.de>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 0 replies; 8+ messages in thread

From: Torsten Zühlsdorff @ 2010-02-11 07:42 UTC (permalink / raw)
  To: pgsql-general@postgresql.org

Thom Brown schrieb:

> 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.

This problem is solved since nearly 5 years with PDO. You can use an
abstraction like DDDBL (see my signature) if you want to save time while
using PDO.

Greetings from Germany,
Torsten

-- 
http://www.dddbl.de - ein Datenbank-Layer, der die Arbeit mit 8 
verschiedenen Datenbanksystemen abstrahiert,
Queries von Applikationen trennt und automatisch die Query-Ergebnisse 
auswerten kann.



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: PHP and PostgreSQL boolean data type
@ 2010-02-12 09:13  Jasen Betts <jasen@xnet.co.nz>
  parent: Thom Brown <thombrown@gmail.com>
  5 siblings, 0 replies; 8+ messages in thread

From: Jasen Betts @ 2010-02-12 09:13 UTC (permalink / raw)
  To: pgsql-php

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?




^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2010-02-12 09:13 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-02-10 12:04 PHP and PostgreSQL boolean data type Thom Brown <thombrown@gmail.com>
2010-02-10 12:11 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
2010-02-10 12:44   ` Thom Brown <thombrown@gmail.com>
2010-02-10 12:13 ` Tommy Gildseth <tommy.gildseth@usit.uio.no>
2010-02-10 14:15 ` Torsten Zühlsdorff <foo@meisterderspiele.de>
2010-02-10 21:56 ` Chris <dmagick@gmail.com>
2010-02-11 07:42 ` Torsten Zühlsdorff <foo@meisterderspiele.de>
2010-02-12 09:13 ` Jasen Betts <jasen@xnet.co.nz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox