PHP PDO-PGSQL and transactions - Mailing list pgsql-general

From Bart Degryse
Subject PHP PDO-PGSQL and transactions
Date
Msg-id 4933B91B.A3DD.0030.0@indicator.be
Whole thread Raw
Responses Re: PHP PDO-PGSQL and transactions  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
I'm having a transaction problem when executing prepared statements using the PHP PDO-PGSQL module.
What happens is this:
On the first $subItem, $checkSubscription goes well, but $insertReminderEntry fails with error
"Duplicate key violates unique constraint"
This error should just be logged and the code should continue with the second $subItem.
And it does, but the second $checkSubscription also fails with error
"Current transaction is aborted, commands ignored until end of transaction block"
And that was not what I meant to happen.
 
Mark that I've only included part of the script. In the foreach loop several other updates and inserts happen.
Basically one of those updates and inserts failing should just be logged and the code should continue.
When the loop has finished all commands that executed without failure should be commited.
 
How should I change my code to make that happen?
Thanks for any help.
----------------------------------------------
$dbh = new PDO('pgsql:host='.$dbIP.';dbname='.$dbName.';port='.$dbPort, $dbUser, $dbPwd, array(PDO::ATTR_PERSISTENT => true));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
$checkSubscription = $dbh->prepare("select uid from subscriptions where uid=:uid and aboname=:subItem");
$insertReminderEntry = $dbh->prepare("insert into reminders (uid,nl) values (:uid,:aboname)");
 
$subscription=array('uktapsps','uktapsem');
$uidArray['uid'] = 'W200705084162';
 
$dbh->beginTransaction();
 
foreach ($subscription as $subItem) {
  $checkSubscription->bindParam(':uid',$uidArray['uid']);
  $checkSubscription->bindParam(':subItem',$subItem);
  try {
    $checkSubscription->execute();
  }
  catch (Exception $e) {
    $msg.="ERROR: Subscription could not be checked! aboname: $subItem\n";
  }
  $insertReminderEntry->bindParam(':uid',$uidArray['uid']);
  $insertReminderEntry->bindParam(':aboname',$subItem);
  try {
    $insertReminderEntry->execute();
  }
  catch (Exception $e) {
    $msg.="ERROR: Entry could not be inserted into the reminder table! aboname: $subItem\n";
  }
}
 

pgsql-general by date:

Previous
From: elekis
Date:
Subject: Cannot open include file: 'nodes/nodes.h'
Next
From: Martijn van Oosterhout
Date:
Subject: Re: PHP PDO-PGSQL and transactions