details of transaction handling in PDO
A transaction (transaction) consists of a sequence of queries and/or UPDATE statements. Begins a transaction with begin, start transaction, rollback rolls back the transaction, commits the transaction. After starting a transaction, you can have several SQL queries or UPDATE statements, and after a SQL submission executes, there should be a statement that determines whether the next step is rolled back, and if it is executed correctly, the transaction is finally committed. Once a transaction is rolled back, the database remains in the pre-transaction state. As if an edited file does not save the file to exit, or to maintain the original appearance of the document. Therefore, a transaction can be treated as an atomic operation, SQL in a transaction, or all executed, or not executed.
In the first two articles, "method of error handling in PDO-errorcode () method", "Method of error handling in PDO two-errorinfo () method" We introduced the method of handling errors in PDO, so let's describe the transaction processing in PDO specifically ~
The functionality of transaction processing can also be implemented in PDO, which is applied in the following ways:
(1) Turn on the transaction--begintransaction () method.
The BeginTransaction () method turns off autocommit (autocommit) mode until the transaction commits or is rolled back.
(2) The Commit transaction--commit () method
The commit () method completes the commit operation of the transaction, returns true successfully, or false.
(3) Transaction rollback--rollback () method
The RollBack () method performs a rollback operation of the transaction.
Add data to the database through the prepare () and execute () methods, and make sure that the data is correctly added to the database by means of a transaction mechanism, with the following steps:
Create a PHP file, first define the database connection parameters, create try{...} Catch{...} Statement, instantiate the PDO constructor in the try{} statement, complete the connection to the database, open the transaction through the BeginTransaction () method, and then define the Insert Add statement, get the data submitted in the form through the $_post[] method, and pass the prepare ( ) and the Execute () method to add data to the database, complete the commit operation of the transaction through commit (0 method, and finally return the error message in the catch{} statement, and perform a rollback operation of the transaction through the RollBack () method, as follows:
<form action= "3.php" Name= "Form1" method= "post" > User name: <input type= "text" name= "username" > Password: <input type= "password" name= "password" > <input type= "Submit" name= "submit" value= "Submit" ></form><?phpheader ("content-type:text/html; Charset=utf-8 "); Set the encoding format of the page $name =$_post[' username ']; $password =$_post[' password '];if ($_post[' username ']!= "" &&$_post[" Password ']!= ') {$dbms = "MySQL"; The type of database $dbName = "PHP_CN"; The database name used $user = "root"; The database user name used $pwd = "root"; The database password used $host = "localhost"; Host name used $DSN = "$dbms: host= $host;d bname= $dbName"; try{$pdo =new PDO ($DSN, $user, $pwd);//Initialize a PDO object to create a database connection object $pdo $pdo-BeginTransaction (); Start transaction $query = "INSERT INTO ' user ' (Username,password) VALUES (' $name ', ' $password ') ';//SQL statement to execute $res = $pdo->prePare ($query);//Prepare query Statement $res->execute (); Executes the query statement and returns the result set if ($res->errorcode ()) {echo "data added successfully"; }else{echo "Data addition failed"; } $pdo->commit (); Execute transaction commit operation}catch (Pdoexception $e) {die ("error!:". $e->getmessage (). ' <br> '); $pdo-RollBack (); Rollback}}?> for performing transactions
The result of the final output is as follows:
Look at the transactions we described above, small partners do not feel very simple, we can contact you to consolidate what you learn, the next we will continue to introduce the storage process in PDO, please read the detailed description of stored procedures in PDO!