2016年12月20日 星期二

PDO transaction

try{ 	 	
$db
->beginTransaction();
 
 $res = .....
if(!$res) throw new Exception(....);
 $db->commit();
}catch (Exception $e){
$db->rollBack();
echo $e->getMessage();
}
------------------------------------------------------
$stmt = $conn->prepare('INSERT INTO my_table(my_id, my_value) VALUES(?, ?)');
$waiting = true; // Set a loop condition to test for
while($waiting) {
    try {
       
$conn->beginTransaction();
        for(
$i=0; $i < 10; $i++) {
           
$stmt->bindValue(1, $i, PDO::PARAM_INT);
           
$stmt->bindValue(2, 'TEST', PDO::PARAM_STR);
           
$stmt->execute();
           
sleep(1);
        }
       
$conn->commit();
       
$waiting = false;
    } catch(
PDOException $e) {
       

           
$conn->rollBack();
            throw
$e;
       
    }
}