Application Analysis Based on mysql transactions, views, stored procedures, and triggers

Source: Internet
Author: User

1. mysql transactions

In MYSQL, only INNODB Data Tables support transaction processing.

There are two ways to start a transaction:

(1)Use begin, rollback, and commit to implement
Copy codeThe Code is as follows:
Start a transaction
Rollback transaction rollback
Commit transaction validation

(2)Directly use set to change the automatic mysql submission Mode
Copy codeThe Code is as follows:
Set autocommit = 0 disable automatic submission
Set autocommit = 1 enable automatic submission

Demo
Copy codeThe Code is as follows:
Header ("Content-type: text/html; charset = UTF-8 ");
Mysql_pconnect ("localhost", "root", "") or die ("database connection failed ");
Mysql_select_db ("test ");
Mysql_query ("set names utf8 ");
// Start a transaction
// Mysql_query ("BEGIN ");
// Mysql_query ("start transaction ");
// Mysql_query ("set autocommit = 1"); // sets that the transaction is not automatically committed. mysql is automatically committed by default.
Mysql_query ("set autocommit = 1"); // start the transaction
$ Sql1 = "insert into 'test' values ('20140901', 'test data ')";
$ Sql2 = "insert into 'test' values ('201312', 'ss', '22')"; // specially written Error
$ Res1 = mysql_query ($ sql1 );
$ Res2 = mysql_query ($ sql2 );
If ($ res1 & $ res2)
{
Mysql_query ("COMMIT ");
Echo "transaction commit ";
} Else {
Mysql_query ("ROLLBACK ");
Echo "transaction rollback ";
}
Mysql_query ("END ");

Related Article

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.