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

From Mark Kirkwood
Subject Re: BUG #5054: PDO -> Query returns "" from Boolean type field, if it has false value.
Date
Msg-id 4AB02350.5030102@catalyst.net.nz
Whole thread Raw
In response to Re: BUG #5054: PDO -> Query returns "" from Boolean type field, if it has false value.  (Mark Kirkwood <mark.kirkwood@catalyst.net.nz>)
List 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

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #5055: Invalid page header error
Next
From: Craig Ringer
Date:
Subject: Re: BUG #5055: Invalid page header error