Saturday, April 8, 2017

Commit Rollback and Get Inserted Id with ADODB



error_reporting (E_ALL ^ E_NOTICE);
require_once('adodb5/adodb.inc.php');
$db = NEWADOConnection('mysqli');
$db->debug = false;
$db->disableBlobs = false;

$host = "localhost";$login="root";$password="password";$database="nexinnov_emgs";
$db->PConnect($host, $login, $password, $database) or die ("Connection failed");

// Start DB-transaction
//$db->autoCommit = false; // According to doc, this should not be needed
//$db->SetTransactionMode("SERIALIZABLE");
$db->StartTrans();

// Update some value
$sql1 = $db->Prepare("insert into table1 (field1) values('A1')");
$sql2 = $db->Prepare("insert into table2 (field2) values('A2')");
$db->Execute($sql1);
if($db->ErrorMsg()){error_log("Error sql1: ".$db->ErrorMsg());}
$db->Execute($sql2);
if($db->ErrorMsg()){error_log("Error sql2: ".$db->ErrorMsg());}

// Do a rollback
$db->FailTrans();
// According to doc, indicates that CompleteTrans will rollback
$db->CompleteTrans();

// Close connection
$db->Close();

No comments:

Post a Comment