Re: [PHP-DB] oci_commit always returns true even when the transaction fails.

[email protected] (ZeYuan Zhang)
Newsgroups php.db
Message-ID <[email protected]>
Thanks everybody.
To: Christopher Jones
thanks for your code. I have run your code.
For the sample you supplied, the oci_commit really returns false.

But if I add a primary key constraint to t_tab.x, and then run the
script, the oci_commit returns true!strange...

So do I need check all the oci_execute's return value? awful...

******** the code pk added ********
<?php

ini_set('display_errors', 'Off');

$c = oci_connect('scott', 'tiger');
if (!$c) {
   $m = oci_error();
   print ('Could not connect to database: '. $m['message']);
}

$stmtarray = array(
       "drop table t_tab",
       "create table t_tab
    ( x int constraint check_x check ( x > 0 ) deferrable initially
immediate primary key,
      y int constraint check_y check ( y > 0 ) deferrable initially deferred)"
);

foreach ($stmtarray as $stmt) {
       $s = oci_parse($c, $stmt);
       $r = oci_execute($s);
       if (!$r) {
               $m = oci_error($c);
               if (!in_array($m['code'], array(   // ignore expected errors
                   942 // table or view does not exist
                   ,  2289 // sequence does not exist
                   ,  4080 // trigger does not exist
                   , 38802 // edition does not exist
               ))) {
                       echo $stmt . PHP_EOL . $m['message'] . PHP_EOL;
               }
       }
}

echo "First Insert\n";
$s = oci_parse($c, "insert into t_tab values ( 1,1 )");
$r = oci_execute($s, OCI_DEFAULT);
if (!$r) {
   $m = oci_error($c);
   print('Could not execute: '. $m['message']);
}
$r = oci_commit($c);
if (!$r) {
   $m = oci_error($c);
   print('Could not commit: '. $m['message']);
}

echo "Second Insert\n";
$s = oci_parse($c, "insert into t_tab values ( 1,-1)");
$r = oci_execute($s, OCI_DEFAULT);  // Explore the difference with and
without OCI_DEFAULT
if (!$r) {
   $m = oci_error($c);
   print('Could not execute: '. $m['message']);
}
$r = oci_commit($c);
if (!$r) {
   $m = oci_error($c);
   print('Could not commit: '. $m['message']);
}

$s = oci_parse($c, "drop table t_tab");
oci_execute($s);

?>

2010/3/17 Christopher Jones <[email protected]>:
>
>> echo "Second Insert\n";
>> $s = oci_parse($c, "insert into t_tab values ( 1,-1)");
>> $r = oci_execute($s, OCI_DEFAULT);  // Explore the difference with and
>> without OCI_DEFAULT
>> if (!$r) {
>>     $m = oci_error($s);
>>     trigger_error('Could not execute: '. $m['message'], E_USER_ERROR);
>> }
>> $r = oci_commit($c);
>> if (!$r) {
>>     $m = oci_error($s);
>
> Correction: the two oci_error() calls after oci_commit() should use
> the $c connection resource, not $s, e.g.:
>
>     $m = oci_error($c);
>
>>     trigger_error('Could not commit: '. $m['message'], E_USER_ERROR);
>> }
>>
>> $s = oci_parse($c, "drop table t_tab");
>> oci_execute($s);
>>
>> ?>
>>
>
> --
> Email: [email protected]
> Tel:  +1 650 506 8630
> Blog:  http://blogs.oracle.com/opal/
> Free PHP Book: http://tinyurl.com/ugpomhome
>



-- 
############################
ZeYuan Zhang
Mail: [email protected]
Blog: http://51ajax.net/
############################
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.