PHP uses the PDO transaction operations database.
Reference article:
Http://php.ncong.com/mysql/pdo/pdo_shiwu.html
On the code:
<!doctype html>PHPTry { $pdo=NewPDO ("Mysql:host=localhost;dbname=test", "root", "XXXXXX"); //set the mode of error usage exception $pdo-SetAttribute (Pdo::attr_errmode, PDO::errmode_exception); //Turn off auto-commit $pdoSetAttribute (pdo::attr_autocommit, 0); } Catch(pdoexception$e) { Echo sprintf("Exception message=%s",$e-getMessage ()); Exit(); } $STRSQL= "UPDATE mytable SET headline= ' haha ' WHERE id=:id"; $pdo-BeginTransaction (); Try { for($i= 1;$i<= 5;$i++) { $stmt=$pdo->prepare ($STRSQL); $affectedRows=$stmt->execute (Array("id" =$i)); if($affectedRows> 0) { Echo sprintf("Update Success id=%s<br/>",$i); } Else { Echo sprintf("Update false id=%s",$i); } } $pdo-commit (); } Catch(pdoexception$e) { Echo sprintf("Exception message=%s",$e-getMessage ()); $pdo-RollBack (); Exit(); } //need to turn on auto-commit $pdo->setattribute (Pdo::attr_autocommit, 1); Echo"; $stm=$pdo->prepare ("Select ID, Headline, create_time from MyTable"); $stm-execute (); $stm->bindcolumn (1,$id); $stm->bindcolumn ("headline",$headline); $stm->bindcolumn (3,$createTime); while($stm->fetch (PDO::fetch_bound)) { Echo"<p>",$id, " - " ,$headline, " - ",$createTime, "</p>"; } ?> </body>[PHP]-PDO transaction operations