Home Backend Development PHP Tutorial Implement MySQL-based transactions with new PHP plug-in_PHP tutorial

Implement MySQL-based transactions with new PHP plug-in_PHP tutorial

Jul 13, 2016 pm 05:35 PM
mysql php affairs transaction processing accomplish open plug-in support of

事务处理支持很长时间以来一直是大多数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 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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Steps to add and delete fields to MySQL tables Steps to add and delete fields to MySQL tables Apr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

How to use MySQL functions for data processing and calculation How to use MySQL functions for data processing and calculation Apr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Detailed explanation of the installation steps of MySQL on macOS system Detailed explanation of the installation steps of MySQL on macOS system Apr 29, 2025 pm 03:36 PM

Installing MySQL on macOS can be achieved through the following steps: 1. Install Homebrew, using the command /bin/bash-c"$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)". 2. Update Homebrew and use brewupdate. 3. Install MySQL and use brewinstallmysql. 4. Start MySQL service and use brewservicesstartmysql. After installation, you can use mysql-u

How to uninstall MySQL and clean residual files How to uninstall MySQL and clean residual files Apr 29, 2025 pm 04:03 PM

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

An efficient way to batch insert data in MySQL An efficient way to batch insert data in MySQL Apr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the advantages of using MySQL over other relational databases? What are the advantages of using MySQL over other relational databases? May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

See all articles