Some useful knowledge about adodb.
Transaction processing
Transaction processing is an important feature of many applications. (For beginners, you can submit a dozen query operations to process transactions, but all these queries are successful or some of them fail.
A classic example is bank processing. Money is transferred from your account and then transferred to another account. If any one of them fails, the entire process must be considered as a failure. Otherwise, the money is drawn out, but not into the account of the other party; or, if the money is not drawn out, the other party's account has a tip for no reason .)
You can perform alert management and control on code-level transactions. The constant error check is used to determine whether to COMMIT (all items of the transaction are correct, executed correctly, and ended) or ROLLBACK (errors in the transaction and all changes need to be restored to the original state ). ADOdb has some useful functions that can properly process operations. The following example adds 10 to the balance of one employee and 10 to the other. There are two queries throughout the process as a complete transaction.
$ Sql1 = "UPDATE employees SET balance = balance-10 WHERE id = 15 ";
$ Sql2 = "UPDATE employees SET balance = balance + 10 WHERE id = 22 ";
$ Db-> StartTrans ();
$ Db-> Execute ($ SQL );
$ Db-> Execute ($ sql2 );
$ Db-> CompleteTrans ();
When you browse the old code, you may see an old method to implement it all. When you use the BeginTrans (), CommitTrans (), and RollbackTrans () functions, you must handle the errors yourself. StartTrans () and CompleteTrans () will automatically handle errors and perform COMMIT or ROLLBACK as appropriate. You can use FailTrans () to force ROLLBACK when necessary.
Of course, you may need to know whether the processing has failed or succeeded. ADOdb provides HasFailedTrans () to implement this function. If an error occurs (or, you can call this case FailTrans (), TRUE is returned; if it succeeds, FALSE is returned.
$ Sql1 = "UPDATE employees SET balance = balance-10 WHERE id = 15 ";
$ Sql2 = "UPDATE employees SET balance = balance + 10 WHERE id = 22 ";
$ Db-> StartTrans ();
$ Db-> Execute ($ SQL );
$ Db-> Execute ($ sql2 );
$ Db-> CompleteTrans ();
If ($ db-> HasFailedTrans ()){
// Something went wrong
}
It is worth noting that your database must support these transaction functions. (Most databases are supported. However, MySQL InnoDB tables are supported, but MySQL MyISAM tables are not supported .)
I hope everything I do will make you very interested in database functions. There are also a large number of interesting functions that can be used to automatically generate HTML from database tables, and the same result can be implemented in different ways. You can find the complete manual here.
Good luck!