search
HomeBackend DevelopmentPHP TutorialImplement MySQL-based transactions with new PHP plug-in_PHP tutorial
Implement MySQL-based transactions with new PHP plug-in_PHP tutorialJul 13, 2016 pm 05:35 PM
mysqlphpaffairstransaction processingaccomplishopenplug-insupportof

事务处理支持很长时间以来一直是大多数MySQL开发者的心愿,随着MySQL 4.0的发布,这个心愿最后终于得以实现。MySQL 4.0后不久,拥有一个新的MySQL插件的PHP 5.x也发布了。这个新插件,MySQL Improved,使得PHP开发者通过利用本地的PHP函数,获得了这些新的事务处理能力。这篇简短的教程将向你说明怎样利用这些新的MySQLi函数,用PHP实现以MySQL为基础的事务。

概要

如果你还不知道,那么我可以告诉你,事务只是一组SQL语句,通常因为它们是彼此相互依赖的,所以要在全有或全无(all-or-nothing)的模式下执行。只有当所有组成的语句都执行成功了,一个事务才算是成功了;任何一个语句中的失败应该都会导致系统“回滚”到它先前的状态,以避免数据连接/崩溃问题。

对于这一点,两个银行帐户间的转帐是一个很好的例子。在数据库级,这样的转帐包括两个步骤:首先,从源帐户中扣除转帐的金额,然后将其加到目标帐户中。如果在第二步中发生了错误,那么第一步就必须被取消,以避免不相符的情况(和愤怒的客户聚众滋事)。事务安全系统将自动地撤到系统先前的“快照”。

大多数数据库(包括MySQL)通过一个命令的组合来完成这个:

* START TRANSACTION命令标志着一个新的事务组的开始。它后面常接一系列的SQL命令。

* COMMIT命令标志着一个事务组的结束,表示事务期间做的所有改变应该被提交或者使之永久化。

* ROLLBACK命令标志着一个事务组的结束,表示事务期间所做的所有改变应该被撤消。

PHP中的事务处理函数

PHP中的MySQLi插件引进了新的函数,帮助开发者利用MySQL的事务处理能力。实质上,这些函数对等地被叫做SQL START TRANSACTION,COMMIT和 ROLLBACK命令。列表A为你展示了一个例子,列表A:


  

  // connect to database

  $dbh = mysqli_connect($host, $user, $pass, $db);

  // turn off auto-commit

  mysqli_autocommit($dbh, FALSE);

  // run query 1

  $result = mysqli_query($dbh, $query1);

  if ($result !== TRUE) {

  mysqli_rollback($dbh); // if error, roll back transaction

  }

  // run query 2

  $result = mysqli_query($dbh, $query2);

  if ($result !== TRUE) {

  mysqli_rollback($dbh); // if error, roll back transaction

  }

  // and so on...

  // assuming no errors, commit transaction

  mysqli_commit($dbh);

  // close connection

  mysqli_close($dbh);

  ?>
 

在PHP 中执行一项事务有三个基本的步骤:

* 第一步是始终关掉数据库的“auto-commit”,它实质上意味着系统在你作出改变时就保存它们。这一点是很重要的,因为在一个事务处理环境中,你应该只有在确定了所有事务处理的“unit”都成功完成了以后,才保存你所做的改变。你可以通过mysqli_autocommit()函数关掉数据库的自动提交。

* 接下来,通过mysqli_query()函数,继续用通常的方法进行INSERT、UPDATE和/或DELETE查询。检验每一个查询返回的值,弄清楚它是否成功了是很重要的。如果其中任何一个查询失败了,mysqli_rollback()函数就会被用来将系统返回到事务进行之前的状态。

* 假设组成事务组的所有命令都成功执行了,就要用mysqli_commit()函数将变化保存到数据库系统。请注意,一旦这个函数被调用,事务就不能被撤消了。

工作实例

要了解这个在实践中是怎么工作的,让我们回到前面讨论过的银行转帐的例子.我们假设你的任务是建立一个简单的Web应用程序,让用户在他们的银行帐户间转帐。我们再进一步假设一个单独用户的帐户存储在一个MySQL数据库中,如下所示:


  mysql> SELECT * FROM accounts;

  +----+------------+---------+

  | id | label | balance |

  +----+------------+---------+

  | 1 | Savings #1 | 1000 |

  | 2 | Current #1 | 2000 |

  | 3 | Current #2 | 3000 |

  +----+------------+---------+

  3 rows in set (0.34 sec)
 

Now, we need to build a simple interface that allows users to enter a cash amount to transfer money from one account to another. The actual "transaction" will be performed with two UPDATE statements, one to take the transfer amount out of the source account, i.e., debit, and the other to credit the transfer amount to the destination account, i.e., credit. Assuming what we are doing is transferring money between accounts, the total available balance across all accounts ($6000) should always remain the same.

Listing B shows possible codes, Listing B:


 

 // connect to database

$dbh = mysqli_connect("localhost", "user", "pass", "test")
or die("Cannot connect");

// turn off auto-commit

mysqli_autocommit($dbh, FALSE);

// look for a transfer

 if ($_POST[submit] && is_numeric($_POST[amt])) {

// add $$ to target account

 $result = mysqli_query($dbh, "UPDATE accounts SET
balance = balance + " . $_POST[amt] . " WHERE id = " . $_POST[to]);

 if ($result !== TRUE) {

mysqli_rollback($dbh); // if error, roll back transaction

 }

// subtract $$ from source account

 $result = mysqli_query($dbh, "UPDATE accounts
SET balance = balance - " . $_POST[amt] .
" WHERE id = " . $_POST[from]);

 if ($result !== TRUE) {

mysqli_rollback($dbh); // if error, roll back transaction

 }

// assuming no errors, commit transaction

mysqli_commit($dbh);

 }

// get account balances

 // save in array, use to generate form

$result = mysqli_query($dbh, "SELECT * FROM accounts");

while ($row = mysqli_fetch_assoc($result)) {

 $accounts[] = $row;

 }

 // close connection

mysqli_close($dbh);

 ?>

As you can see, the script starts by connecting to the database and turning off auto-commit. Then execute a SELECT query to retrieve the cash receipts and payments for all accounts, and then construct a table with a drop-down interface to select the source/target account to be used for the transaction. Exhibit A shows the original form.


Initial form

Once the form is completed and submitted, the two UPDATE queries start actually performing the debit and credit operations. Note that each query has a mysqli_rollback() at the end, which will be activated if the query fails. Assuming no query fails, the new income and expenditure table is stored in the database by calling mysqli_commit(). At that point the database connection is closed.

You can try it yourself and transfer $500 from Savings #1 to Current #2. Once you perform the transfer, you will see the new balance sheet results as shown in Exhibit B.


The status after the transaction is completed.

Tip: Of course, this is just a simple two-command transaction. Usually, you can use this transaction model when there are many SQL statements to be executed together, and the failure of one statement has a cascading effect on other statements. In these cases, you may find it easier to condense the calls to mysqli_query() and mysqli_rollback() into a single user-defined function that can be called when needed.

As you can see, implementing a transaction processing model with PHP and MySQL can make your MySQL database more robust to query execution errors. However, before you start rewriting code and using this model, it is worth noting that transactions do increase the cost of system performance management, so it is always a good idea to do a cost-benefit analysis before implementing this model.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508371.htmlTechArticleTransaction processing support has been the wish of most MySQL developers for a long time. With the release of MySQL 4.0, This wish finally came true. Shortly after MySQL 4.0, there was a new...
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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),