Re: BUG #5054: PDO -> Query returns "" from Boolean type field, if it has false value.

From: Mark Kirkwood <mark(dot)kirkwood(at)catalyst(dot)net(dot)nz>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Yujin <aloudnoise(at)mail(dot)ru>, pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #5054: PDO -> Query returns "" from Boolean type field, if it has false value.
Date: 2009-09-15 23:29:20
Message-ID: 4AB02350.5030102@catalyst.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
>
>
> Trying out some code with Php 5.3.1-dev:
>
> $sql = "SELECT false";
> $stmt = $dbh->query($sql);
> $result = $stmt->fetch(PDO::FETCH_NUM);
> print(" " . $result[0] . "\n");
>
> reproduces what Yujin is seeing...

After a bit of digging through the PDO code, I see what is happening.
the ->fetch operation is returning a Php boolean correctly from PDO (you
can use var_dump($result[0]) to check this), but when you use print as
above, Php casts the boolean to string - and for whatever reason Php
reckons turning (boolean)false into (string)"" is the way to go. So to
get a sensible result you need to do something like:

$result = $stmt->fetch(PDO::FETCH_NUM);
print(" " . (integer) $result[0] . "\n");

i.e: explicit cast on the result value.

This is confusing and seemingly not consistent - for instance the Mysql
version of this example returns a string "0" from PDO, so gives a 0 for
false in a more expected/intuitive way...

regards

Mark

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Craig Ringer 2009-09-16 00:23:27 Re: BUG #5055: Invalid page header error
Previous Message Tom Lane 2009-09-15 23:21:06 Re: BUG #5055: Invalid page header error