Home >Backend Development >PHP Tutorial >Transaction processing analysis of PDO in PHP, phppdo transaction processing_PHP tutorial

Transaction processing analysis of PDO in PHP, phppdo transaction processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:54:22868browse

Transaction processing analysis of PDO in PHP, phppdo transaction processing

This article analyzes the transaction processing of PDO in PHP with examples. Share it with everyone for your reference, the details are as follows:

Transaction processing has four characteristics: atomicity, consistency, independence, and durability.

Not all databases support transaction processing. PDO provides transaction support for databases that can perform transaction processing.

Note when configuring transaction processing:

1. Turn off automatic submission of PDO;

$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT, false);

2. Methods required to open a transaction;

$pdo->beginTransaction(); // 开启一个事务
$pdo->commit(); // 提交事务
$pdo->rollback(); // 回滚事务

3. General transaction processing is run in the try...catch... statement. When the transaction fails, the catch code segment is executed.

<&#63;php
try {
  $pdo->beginTransaction(); // 开启一个事务
  $row = null;
  $row = $pdo->exec("xxx"); // 执行第一个 SQL
  if (!$row)
    throw new PDOException('提示信息或执行动作'); // 如出现异常提示信息或执行动作
  $row = $pdo->exec("xxx"); // 执行第二个 SQL
  if (!$row)
    throw new PDOException('提示信息或执行动作');
  $pdo->commit();
} catch (PDOException $e) {
  $pdo->rollback(); // 执行失败,事务回滚
  exit($e->getMessage());
}
&#63;>

If an error occurs in the SQL statement in the transaction, all SQL will not be executed. Only when all SQL statements are correct will they be submitted for execution.

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP database operation skills based on pdo", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "PHP basic Grammar introductory tutorial", "php office document operation skills summary (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) Usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • Some supplements to solve the Chinese garbled problem with PDO in PHP
  • php PDO Chinese garbled solution
  • PHP Analysis of examples of common PDO class libraries
  • A simple example of PDO operation in PHP
  • A simple way to use PDO in PHP5.2
  • The PDO method in php realizes the addition, deletion, modification and query of the database
  • Detailed explanation of the use of PDO, the mysql connection method in php
  • Comparative analysis of the database connection method pdo and mysqli in php
  • A list of output results of various parameters of PHP PDO fetch mode
  • Detailed explanation of the method of using PDO in PHP
  • Solution to the garbled problem of using PDO to operate the database in PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119988.htmlTechArticleTransaction processing analysis of PDO in PHP, phppdo transaction processing This article analyzes the transaction processing of PDO in PHP with examples. Share it with everyone for your reference, the details are as follows: Transaction processing has four characteristics:...
Statement:
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