本文實例講述了php PDO實現的事務回滾。分享給大家供大家參考,具體如下:
$servername="localhost";$username="root";$password="admin";$dbname="test";try{ $conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password); $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //開始事務 $conn->beginTransaction(); $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','XIAMING','yexianming@163.com')"); $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','CONG','yecong@163.com')"); $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('FANG','MENG','fangmeng@168.com')"); //提交事務 $conn->commit(); echo "New records created successfully!";}catch(PDOException $e){ //回滾事務 $conn->rollBack(); echo $sql."<br>".$e->getMessage();}$conn=NULL;
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選