Skip to Main Content

DevOps, CI/CD and Automation

oci_bind_by_name binds boolean false as NULL

dregadMay 13 2014 — edited May 13 2014

Hi,

I'm trying to insert a PHP boolean variable into a column defined as number. It works fine for TRUE, but fails with ORA-01400 for FALSE.

Reason for using NUMBER(1) is for portability with other DB engines.

create table test (version varchar(10), obsolete number(1) not null);

<?php
$conn = oci_connect("user", "password", "localhost/XE");

$sql = "INSERT INTO test (version, obsolete) values (:0, :1)";
$param = array( 'test', false );

$stmt=oci_parse($conn, $sql);
oci_bind_by_name($stmt, ":0", $param[0]);
oci_bind_by_name($stmt, ":1", $param[1]);
var_dump(oci_error($conn));

var_dump(oci_execute($stmt));

$ php /tmp/test.php
bool(false)
PHP Warning:  oci_execute(): ORA-01400: cannot insert NULL into ("USER"."TEST"."OBSOLETE") in /tmp/test.php on line 12
bool(false)

If I cast to (int) prior to binding the boolean it works so I have an obvious workaround, but my expectation would be that false would be converted to 0, just like true is automatically stored as 1 (with  $param = array( 'test', true );, the execution is successful)

$ php /tmp/test.php
bool(false)
bool(true)

SQL> select * from test;

VERSION      OBSOLETE
---------- ----------
test                1

Is this a bug ? If not, can someone explain why this happens and maybe what I'm doing wrong ?

Cheers

Damien

------

PHP 5.5.9-1ubuntu4

OCI8 Version => 2.0.8
Oracle XE 11.2.0.2.0

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 10 2014
Added on May 13 2014
1 comment
2,923 views