Home  >  Article  >  Backend Development  >  Detailed explanation of php transaction processing examples, detailed explanation of php transaction processing_PHP tutorial

Detailed explanation of php transaction processing examples, detailed explanation of php transaction processing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:23751browse

Detailed explanation of php transaction processing examples, detailed explanation of php transaction processing

1. Overview of PHP transaction processing:

Transaction: is a collection of several events
Transaction processing: The transaction will be executed only when all events are executed successfully; if any event cannot be executed successfully, other events of the transaction will not be executed.

As long as your MySQL version supports BDB or InnoDB table types, then your MySQL has transaction processing capabilities. Among them, the InnoDB table type is the most used. Although things happened later that made MySQL unhappy, such as Oracle's acquisition of InnoDB, such business events have nothing to do with technology. The following takes the InnoDB table type as an example. Let’s briefly talk about transaction processing in MySQL.

2. PHP transaction processing code:

<&#63;php
 try{
 $pdo=new PDO("mysql:host=localhost;dbname=psp","root","");
 $pdo->exec("set names utf8");
 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);//设置异常处理模式
 $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);//关闭自动提交
 }catch(PDOException $e){
 echo "数据库连接失败";
 exit;
 }

 try{
 $age=10;
 $pdo->beginTransaction();//开始事务
 $affected_rows1=$pdo->exec("update kfry set k_age=k_age+{$age} where k_name='user1'");
 $affected_rows2=$pdo->exec("update kfry set k_age=k_age-{$age} where k_name='user2'");//随意更改使之执行成功或失败
 /* if($affected_rows1&&$affected_rows2)
 {
  $pdo->commit();
  echo "操作成功";
 }else{
  $pdo->rollback();
 } */
 if(!$affected_rows1)
  throw new PDOException("加入错误");
 if(!$affected_rows2)
  throw new PDOException("减少错误");
 echo "操作成功";
 $pdo->commit();//如果执行到此处前面两个更新sql语句执行成功,整个事务执行成功
 }catch(PDOException $e){
 echo "操作失败:".$e->getMessage();
 $pdo->rollback();//执行事务中的语句出了问题,整个事务全部撤销
 }
 $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
 //测试是否成功
 echo "\n操作结果为:\n";
 $sql="select * from kfry";
 $result=$pdo->query($sql);
 foreach($result as $v)
 {
 echo $v['k_name']." ".$v['k_age']."\n";
 }
&#63;>

How PHP generally performs SQL transaction processing

There is transaction processing in EC. I have never studied how to implement this in PHP. I will tell you after I go home and study it

php leveling steps

The first lecture of the first phase, WEB basics 1.1 Basic knowledge of websites; 1.2 Introduction to network protocols; 1.3 The difference between B/S and C/S structures; 1.4 Introduction to WEB programming and website development technology. Lecture 2, web design 2.1 Introduction and use of Dreamweaver; 2.2 Static web page HTML language; 2.3 Titles and paragraphs, line breaks and dividing lines; 2.4 Tables and forms; 2.5 Frames, hyperlinks, and pictures. Examples: 1. Use tables to design web page layouts; 2. Use forms, tables, and frames to design system backend interfaces. The third lecture, DIV+CSS 3.1 The concept and basic syntax of CSS; 3.2 Use CSS to set rich text effects and image effects; 3.3 Use CSS to set the styles of tables and forms, and use CSS to set page and browser elements; 3.4 CSS boxes Model; 3.7 CSS+DIV layout, CSS+DIV beautification and layout practice. Examples: 1. Use DIV+CSS to design buttons and tabs; 2. Use DIV+CSS to design navigation menus; 3. Use DIV+CSS to design forum web pages; 4. Use DIV+CSS to design blog web pages. Lecture 4, PHP development environment 4.1 Installation of integrated development environment XAMPP; 4.2 Installation and configuration of Zend Studio; 4.3 Installation and configuration of Eclipse PDT; 4.4 Writing the first PHP program; 4.5 Debugging PHP program. Lecture 5, PHP programming basics 5.1 PHP language features and development trends; 5.2 PHP variable constant data types; 5.3 PHP operators and expressions; 5.4 PHP flow control statements; 5.5 PHP functions. Lecture 6, MySQL development basics 6.1 Introduction and installation of MySQL database system; 6.2 Introduction to MySQL data types; 6.3 Creation, modification and deletion of MySQL tables; 6.4 MySQL query statements; 6.5 Detailed explanation of the use of PHP MySQL functions. Examples: 1. Forum database table design; 2. PHP connects to MySQL database to implement addition, deletion, modification and query. Lecture 7, Web2.0 development technology Ajax 7.1 Introduction to JavaScript; 7.2 JavaScript syntax foundation; 7.3 DOM object foundation and events; 7.4 Detailed explanation of Ajax core object XMLHttpRequest; 7.5 Ajax asynchronous communication principle; 7.6 Developing Ajax applications; 7.6 Introduction to Ajax framework jQuery and use. Examples: 1. The dynamic switching effect of Tab; 2. Use Ajax in the forum system to verify whether the user name is registered; 3. Obtain user information through Ajax in the forum system. Lecture 8, PHP Web2.0 website example development 8.1 Web2.0 style forum system development; 8.2 Web2.0 style blog system practical development. Examples: 1. Comprehensive DIV+CSS +PHP+MySQL+Ajax technology development forum; 2. Comprehensive DIV+CSS +PHP+MySQL+Ajax technology development blog system. The first lecture of the second phase, PHP object-oriented basics 1.1 Introduction to object-oriented programming; 1.2 The difference between process-oriented and object-oriented; 1.3 Basic characteristics of object-oriented; 1.4 Classes, properties and methods; 1.5 Constructors; 1.6 Instantiation of classes; 1.7 Using attributes and methods of classes; 1.8 Access control of classes; 1.9 Inheritance and polymorphism of classes; 1.10 Object-oriented interfaces and abstract classes; 1.11 Classes, objects and the relationship between objects; 1.12 Several common operators and Keywords. Examples: 1. Shopping cart object relationship design; 2. Class, student, course, and exam object relationship design. The second lecture, PHP template technology Smarty framework 2.1 Introduction to Smarty template technology; 2.2 Smarty installation and configuration; 2.3 Smarty variables and variable modifiers; 2.4 Smarty branch structure; 2.5 Smarty loop... The rest of the full text >>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840632.htmlTechArticleDetailed explanation of php transaction processing examples, detailed explanation of php transaction processing 1. Overview of php transaction processing: Transaction: is a collection of several events Transaction processing: The transaction is executed when all events are executed successfully; if any...
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