Mysql transaction processing code-PHP source code

Source: Internet
Author: User
The PHP code for implementing transaction processing data can also be described as the mysql transaction processing code. The PHP code for implementing transaction processing data can also be described as the mysql transaction processing code.

Script ec (2); script

Public function insertUser ($ userArray ){
Foreach ($ userArray as $ key => $ value ){
@ $ Field. = "$ key ,";
@ $ Content. = "'$ value ',";
}
$ Field = ereg_replace (', $', '', $ field );
$ Content = ereg_replace (', $', '', $ content );

$ Db = db_connect (); // database connection tutorial

$ Db-> autocommit (FALSE); // set to non-automatic commit-Transaction Processing

$ Sql1 = "insert into t_user (". $ field. ") VALUES (". $ content .")";
$ Result1 = $ db-> query ($ sql1 );

$ Sql2 = "insert into t_userpost (f_username) VALUES ('". $ userArray ['f _ username']. "')";
$ Result2 = $ db-> query ($ sql2 );

If ($ result1 & $ result2 ){
$ Db-> commit (); // all succeeded. The execution result is submitted.
Echo 'Submit ';
} Else {
$ Db-> rollback (); // If any error occurs, roll back and cancel the execution result.
Echo 'rollback ';
}
$ Db-> close ();
}

We will use commit to provide transactions and rollback failures.

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.