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.