php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #44791 pg_execute() works with boolean params and "true" but not with "false"
Submitted: 2008-04-21 09:14 UTC Modified: 2009-05-04 23:58 UTC
From: php at benjaminschulz dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.3CVS-2008-04-21 (CVS) OS: osx
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at benjaminschulz dot com
New email:
PHP Version: OS:

 

 [2008-04-21 09:14 UTC] php at benjaminschulz dot com
Description:
------------
Binding a boolean "false" to a statement doesn't work.

Reproduce code:
---------------
$result = pg_prepare($dbconn, "my_query", 'SELECT * FROM test WHERE boolfield = $1');
$result = pg_execute($dbconn, "my_query", array(true)); // works
$result = pg_execute($dbconn, "my_query", array(false)); // triggers an error

Expected result:
----------------
No error ;)


Actual result:
--------------
Warning: pg_execute(): Query failed: ERROR:  invalid input syntax for type boolean: "" in ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-21 09:32 UTC] pajoye@php.net
The problem is due to the way the casting is done internally:

      SEPARATE_ZVAL(tmp);
      convert_to_string_ex(tmp);
      if (Z_TYPE_PP(tmp) != IS_STRING) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
        _php_pgsql_free_params(params, num_params);
        RETURN_FALSE;
      }

It could be improved by testing the variable type prior to convert. However I'm not sure if it is desired or if it has some bad side effects like BC breaks.
 [2008-04-21 09:35 UTC] php at benjaminschulz dot com
This applies to pg_send_execute, too.
 [2009-05-04 23:58 UTC] mbeccati@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

I think the behaviour is exactly what I would expect. PostrgeSQL booleans are "t" and "f". Recent PgSQL versions also accept 1 and 0 as input for booleans, but not the empty string, which is what PHP outputs for a bool(false).

The pgsql PHP extension wasn't written with portability in mind, so it expects users to have knowledge of how PostgreSQL works. In this specific case, prepare/execute don't do any type of magic and just cast the parameters to string.

Your example should in fact look like:
$result = pg_execute($dbconn, "my_query", array('t'));
$result = pg_execute($dbconn, "my_query", array('f'));

I've been pointed to this bug by Pierre and discussed with him about it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 10:01:31 2024 UTC