Php mysql transaction processing Implementation Program

Source: Internet
Author: User

The condition for implementing this function is that the table type that the Environment mysql 5.2/php 5 supports transactions needs InnoDB. With these conditions, you can implement the above, this rollback operation is often used by large projects, such as banks and e-commerce. If you need it, you can refer to it.

Recently, the Project software has been upgraded to support transaction processing. An example is provided for your reference.
Environment mysql 5.2/php 5
InnoDB is required for table types that support transactions.

The Code is as follows: Copy code

<? PHP
$ LinkID = mysql_connect ('localhost: 100', 'root ',*******);
Mysql_select_db ('web _ his ', $ LinkID );
Mysql_query ("set names utf8 ");

/* Create a transaction */
Mysql_query ('start transaction') or exit (mysql_error ());
$ Ssql1 = "insert into pf_item values ('22', 'ours ', '30')"; // Execute SQL 1
If (! Mysql_query ($ ssql1 )){
Echo $ ssql1.mysql _ errno (). ":". mysql_error (). "<br> ";
Mysql_query ('rollback') or exit (mysql_error (); // determines whether to roll back When execution fails.

Exit;
}
$ Ssql1 = "insert into pf_item values ('21', 'hangel', '10')"; // Execute SQL 2
If (! Mysql_query ($ ssql1 )){
Echo $ ssql1.mysql _ errno (). ":". mysql_error (). "<br> ";
Mysql_query ('rollback') or exit (mysql_error (); // determines whether to roll back When execution fails.

Exit;
}

Mysql_query ('commit ') or exit (mysql_error (); // execute the transaction

Mysql_close ($ LinkID );
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.