Example code sharing of how to implement transaction processing in PHP based on PDO

黄舟
Release: 2023-03-14 11:14:01
Original
1254 people have browsed it

This article mainly introduces the transaction processing method based on pdo in PHP, and analyzes the related implementation techniques of PDO using pdo for transaction operations in the form of examples. Friends in need can refer to the following

The examples in this article describe PHP implements pdo-based transaction processing method. Share it with everyone for your reference, the details are as follows:

Example 1:

try {} catch () {} form


<?php
$dsn = &#39;mysql:dbname=cheyun_cms;host=127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;111111&#39;;
//采用预处理+事务处理执行SQL操作
//1.连接数据库
try {
  $pdo = new PDO($dsn, $user, $password);
  $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  die("数据库连接失败".$e->getMessage());
}
//2.执行数据操作
try{
  //开启事物,此时会关闭自动提交
  $pdo->beginTransaction();
  $sql = "insert into cy_log (logid, value, action, file) values (?, ?, ?, ?)";
  $stmt = $pdo->prepare($sql);
  //传入参数
  $stmt->execute(array(null,"test4","w",11));
  $stmt->execute(array(null,"test5","w",11));
  $stmt->execute(array(null,"test3","w",11));
  //提交事物,并且 数据库连接返回到自动提交模式
  $pdo->commit();
}catch(PDOException $e){
  echo &#39;执行失败&#39;.$e->getMessage();
  //如果数据库被设置成自动提交模式,rollback 在回滚事务之后将恢复自动提交模式。
  //包括 MySQL 在内的一些数据库, 当在一个事务内有类似删除或创建数据表等 DLL 语句时,会自动导致一个隐式地提交。
  //隐式地提交将无法回滚此事务范围内的任何更改。即 DDL 语句无法回滚
  $pdo->rollback();
}
Copy after login

Example 2:

if...else...form


<?php
$dsn = &#39;mysql:dbname=cheyun_cms;host=127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;111111&#39;;
//采用预处理+事务处理执行SQL操作
//1.连接数据库
try {
  $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
  die("数据库连接失败".$e->getMessage());
}
//2.执行数据操作
//开启事物
$pdo->beginTransaction();
$sql = "insert into cy_log (logid, value, action, file) values (?, ?, ?, ?)";
$stmt = $pdo->prepare($sql);
$datalist = array(
  array(null,"test9","w",11),
  array(null,"test10","w",11),
  array(null,"test11","w",11)
);
//是否提交标志位
$isCommit = true;
foreach($datalist as $data){
  $stmt->execute($data);
  if($stmt->errorCode()>0){
    //回滚
    $pdo->rollback();
    $isCommit = false;
    break;
  }
}
if($isCommit){
  //提交事物
  $pdo->commit();
}
Copy after login

Note:

The data table requires InnoDB type

The above is the detailed content of Example code sharing of how to implement transaction processing in PHP based on PDO. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template