|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-07-27 00:14 UTC] php at sagi dot org
Description:
------------
Running latest php5 snapshot (php5-200507261230), PDO connected to pgsql 8.0 server.
I'm trying to run a query similar to this:
$res = $db->prepare('SELECT id FROM table WHERE mybool = ?');
$res->execute(array(false));
PDO throws this exception: 'SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type boolean: ""'
The query that has been executed, according to the server log, is: "SELECT id FROM table WHERE mybool = ''"
Which is obviously not right. When trying to run the same query with bool(true) parameter, PDO correctly quotes it as '1'.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 10:00:02 2025 UTC |
I know how string casting works, but this is not a string. PDO knows, for example, how to convert PHP NULL to SQL NULL and not string('') (like string casting does). Why can't it cast bool values to an integer instead? This behavior is bad. PDO knows how to cast the value to real php bool when selecting, but cannot cast it back when inserting/updating, which means a simple attempt to re-insert a row that has just been selected, using the same object, fails.Try this as a workaround for now: $res = $db->prepare('SELECT ...', array( PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true )); You can blame the pretty poor native prepared statement API in pgsql for this one; it just doesn't tell PDO anything about the parameter types so it can't make an informed decision about how to treat the parameters.Nope, still get the same exception and the same query is being executed according to the server log. Still using the same php5-200507261230 snapshot. The exact code: $res = $db->prepare( 'SELECT id,name,trial FROM shops WHERE trial = ?', array(PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT => true) ); $res->execute(array(false)); And the result: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for type boolean: ""' in /home/shopy/dev/tmp/test.php:12 Stack trace: #0 /home/shopy/dev/tmp/test.php(12): PDOStatement->execute(Array) #1 {main} thrown in /home/shopy/dev/tmp/test.php on line 12I "fixed" this by binding the parameters as integers: $bool = false; $st = $db->prepare( "insert into foo ( bar ) values ( :bar )" ); $st->bindParam( ":bar", $boo, PDO_PARAM_INT ); $st->execute(); Add the following to your pgsql: -- begin CREATE OR REPLACE FUNCTION int_to_bool(int4) RETURNS bool AS $BODY$ select case when $1 <> 0 then TRUE else FALSE end; $BODY$ LANGUAGE 'sql' VOLATILE; CREATE CAST (int4 AS bool) WITH FUNCTION int_to_bool(int4) AS ASSIGNMENT; -- end PDO converts true/false to 1/0 which pass into the cast function. Couldn't get an implicit ''::bool cast working. e.g., CREATE OR REPLACE FUNCTION text_to_bool(text) RETURNS bool AS $BODY$ select case when $1 <> '' then TRUE else FALSE end; $BODY$ LANGUAGE 'sql' VOLATILE; CREATE CAST (text AS bool) WITH FUNCTION text_to_bool(text) AS ASSIGNMENT; And then doing "select ''::bool;" didn't work. Doin "select ''::text::bool" did work, though, so you could factor that into your PDO statements, too: insert into foo(bar) values(:bar::text); which will call the above cast ('' -> text -> bool).