/** things handle linear operations. * Take money transfer as an example*/ Header(' Content-type:text/html;charset=utf-8 '); $opt=Array(Pdo::attr_persistent =TRUE); $dsn= "mysql:dbname=0328;host=127.0.0.1"; $user= "Root"; $password= ' '; $DBH=NewPDO ($dsn,$user,$password,$opt); $DBH->setattribute (pdo::attr_autocommit,0);//Turn off auto-commit Try{ $DBH-BeginTransaction (); $price= 10; $is _acheived=$DBH-exec("Update account set money=money-$pricewhere Name= ' Wang He Jun ' "); if($is _acheived> 0){ Echo' Wang He Jun successfully turned out '.$price. ' Yuan <br> '; }Else{ Throw NewPdoexception ("Turn out failed! <br> "); } $is _acheived=$DBH-exec("Update account set money=money+$priceWhere Name= ' King II ' "); if($is _acheived> 0){ Echo' Wang er successfully transferred '.$price. ' Yuan <br> '; }Else{ Throw NewPdoexception ("Transfer to failure! <br> "); } Echo' Trading Success! ‘; $DBH-commit (); }Catch(pdoexception$e){ Echo"Transaction failed".$e-GetMessage (); $DBH-rollback (); } $DBH->setattribute (pdo::attr_autocommit,1);//turn on auto-commit
PDO + Transactional Processing linear transactions