PHP uses MySQL transaction instance to parse _php tips

Source: Internet
Author: User

This article illustrates the example of PHP using MySQL, and provides a detailed explanation with annotations. Share for everyone to use for reference.

The specific examples are as follows:

<?php
//database connection
$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' Test ', $conn);
mysql_query ("SET NAMES GBK");
/* The table that supports the transaction must be a InnoDB type
can only occur once in a transaction:
mysql_query (' Start TRANSACTION ');/start transaction
mysql_query (' ROLLBACK ');/ROLLBACK TRANSACTION
mysql_query (' commit ');/COMMIT TRANSACTION If a transaction is rolled back more than once in a transaction

, then all operations on the database are canceled until the transaction is committed and only before the first rollback to the start transaction. All database operations will remain in effect until the transaction is committed after the first rollback, so the rollback statement is typically placed only before the transaction statement is committed
if a transaction has no commit statement, all of the following database operations are performed from the start of the transaction (the method return is incorrect) but have no effect on the database. However, in the execution of the next paragraph start transaction statement, the first section of the transaction automatically submitted
* * *
mysql_query (' Start TRANSACTION ');
$isBad = 0;

$ins _testtable1 = "INSERT into Testtable1 (name,age) VALUES (' a ')";
if (!mysql_query ($ins _testtable1)) {
  $isBad =1;
}
Error inserting statement field name
$ins _testtable2 = "INSERT into Testtable1 (name,ages) VALUES (' second ', ')";
if (!mysql_query ($ins _testtable2)) {
  $isBad =1;
}
if ($isBad = = 1) {
  echo $isBad;
  mysql_query (' ROLLBACK ');
}
mysql_query (' COMMIT ');
Mysql_close ($conn);
? >

It is hoped that the examples mentioned in this paper will help us to learn php+mysql programming.

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.