News新闻

业界新闻动态、技术前沿
Who are we?

您的位置:首页      网络营销      php事务回滚简单实现方法示例

php事务回滚简单实现方法示例

本文实例讲述了php事务回滚简单实现方法。分享给大家供大家参考,具体如下:$servername="localhost";$username="root";$password="admin";$dbname="test";try{$conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);$conn-setAttribute(PDO::AT

本文实例讲述了php事务回滚简单实现方法。分享给大家供大家参考,具体如下:

$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;
    我要评论
    共有 人参与,评论 条,顶帖 [点击查看]
  • 验证码:
  • 所有评论仅代表网友意见,与本站立场无关!