时间:2021-07-01 10:21:17 帮助过:29人阅读
具体如下:
$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','1111@163.com')"); $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','CONG','2222@163.com')"); $conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('FANG','MENG','3333@163.com')"); //提交事务 $conn->commit(); echo "New records created successfully!"; }catch(PDOException $e){ //回滚事务 $conn->rollBack(); echo $sql."<br>".$e->getMessage(); } $conn=NULL;
相关推荐:
php 数据库开发你要知道的三点
PHP之详解PDO
PHP中关于PDO数据访问抽象层的功能操作的介绍
以上就是PHP操作数据库之事务回滚实例的详细内容,更多请关注Gxl网其它相关文章!