Use PHP to operate MySQL transaction instances and mysql transaction instances

Source: Internet
Author: User
Tags mysql functions php mysql

Use PHP to operate MySQL transaction instances and mysql transaction instances

This example describes how to operate MySQL transactions in PHP. The specific method is as follows:

Generally, transactions must have ACID characteristics. ACID is the first letter of four words: Atomic (Atomicity), Consistent (consistency), Isolated (isolation), and Durable (continuity, the following uses bank transfers as an example to describe their meanings:

① Atomicity: the statements that constitute Transaction Processing Form A logical unit and cannot only execute a part of them. In other words, transactions are the smallest units that are inseparable. For example, in the bank transfer process, it is unreasonable to change only one account by subtracting the transfer amount from one account and adding it to another account.
② Consistency: the database is consistent before and after transaction processing. That is to say, the transaction should correctly switch the system status. For example, in the bank transfer process, either the transfer amount is transferred from one account to another, or both accounts remain unchanged.
③ Isolation: one transaction has no impact on the processing of another transaction. That is to say, no transaction can see a transaction in an incomplete state. For example, in the bank transfer process, another transfer transaction can only be in the waiting state before the transfer transaction is committed.
④ Continuity: the effect of transaction processing can be permanently saved. Conversely, transactions should be able to withstand all failures, including server, process, communication, and media failures. For example, in the bank transfer process, the account status must be saved after the transfer.

In PHP, mysqli has well encapsulated mysql transaction-related operations. Example:
Copy codeThe Code is as follows: $ sql1 = "update User set ScoreCount = ScoreCount + 10 where ID = '20140901 '";
$ Sql2 = "update ScoreDetail set FScore = 300 where ID = '000000 '";
$ Sql3 = "insert into ScoreDetail ID, Score) values ('000000', 60 )";
$ Mysqli = new mysqli ('localhost', 'root', '', 'db _ Lib2Test ');
$ Mysqli-> autocommit (false); // start transaction
$ Mysqli-> query ($ sql1 );
$ Mysqli-> query ($ sql2 );
If (! $ Mysqli-> errno ){
$ Mysqli-> commit ();
Echo 'OK ';
} Else {
Echo 'err ';
$ Mysqli-> rollback ();
}
Here, we use php mysql functions to execute transactions.
Copy codeThe Code is as follows: $ sql1 = "update User set ScoreCount = ScoreCount + 10 where ID = '20140901 '";
$ Sql2 = "update ScoreDetail set FScore = 300 where ID = '000000 '";
$ Sql3 = "insert into ScoreDetail ID, Score) values ('000000', 60 )";
$ Conn = mysql_connect ('localhost', 'root ','');
Mysql_select_db ('db _ Lib2Test ');
Mysql_query ('start transaction ');
// Mysql_query ('set autocommit = 0 ');
Mysql_query ($ sql1 );
Mysql_query ($ sql2 );
If (mysql_errno ()){
Mysql_query ('rollback ');
Echo 'err ';
} Else {
Mysql_query ('commit ');
Echo 'OK ';
}
// Mysql_query ('set autocommit = 1 ');
// Mysql_query ($ sql3 );

Note:

MyISAM: transactions are not supported and used by read-only programs to improve performance.
InnoDB: supports ACID transactions, row-level locks, and concurrency.
Berkeley DB: supports transactions.

I hope this article will help you design the PHP + MySQL database program.


Who gives a php to operate mysql class and has detailed instructions or examples?

The following is a simple database encapsulation class for php5. It is suitable for learning. You can add other operations such as deletion and update:
<? Php
Class Mysql {// define a class with uppercase letters at the beginning
Public $ host; // server name. The access modifier PUBLIC certifies that $ host is a public entity that can be accessed from inside and outside the class and can be inherited.
Public $ user; // user name, which is a public attribute
Private $ pass; // password. The modifier private proves that $ pass is private. It can only be used inside the class and cannot be inherited.
Public $ dbname; // database name, which is also a public attribute.
// _ Construct: This is a build function that defines some initial information. There are three parameters.
Public function _ construct ($ host, $ user, $ pass, $ dbname ){
$ This-> host = $ host;
$ This-> user = $ user;
$ This-> pass = $ pass;
$ This-> dbname = $ dbname;
$ Link = @ mysql_connect ($ this-> host, $ this-> user, $ this-> pass)
Or die ("error ");
@ Mysql_select_db ($ this-> dbname, $ link)
Or die ("error2 ");
}
// Define the database query and display functions
Function myQuery ($ SQL ){
$ Result = mysql_query ($ SQL );
If (! $ Result ){
Echo "error3 ";
Exit;
}
$ Num = mysql_num_rows ($ result );
If ($ num ){
Echo "NO". $ num;
}
While ($ row = mysql_fetch_assoc ($ result )){
Echo '<tr> <td bgcolor = "# fffddd"> <pre> '.html specialchars (stripslashes ($ row ['body']). "<pre> </td> </tr> ";
}
}
}
$ Rutt = new Mysql ('localhost', 'root', 'sss', 'calvin '); // instantiate a class... remember that the parameters here are the same as those of the constructor...
$ Rutt-> myQuery (... the remaining full text>

I want to develop a website with PHP programming code. How can I implement PHP MYSQL cache? For example

// The following are cache classes:
<? Php
Class cache {
// Cache directory
Var $ cacheRoot = "./cache /";
// Cache Update time in seconds. 0 indicates no cache.
Var $ cacheLimitTime = 0;
// Cache file name
Var $ cacheFileName = "";
// Cache Extension
Var $ cacheFileExt = "html ";

/*
* Constructor
* Int $ cacheLimitTime cache Update Time
*/
Function cache ($ cacheLimitTime ){
If (intval ($ cacheLimitTime ))
$ This-> cacheLimitTime = $ cacheLimitTime;
$ This-> cacheFileName = $ this-> getCacheFileName ();
// Echo $ this-> cacheFileName;
Ob_start ();
}

/*
* Check whether the cached files are updated within the specified Update time.
* Return: if the file content is returned within the update time, otherwise, an error is returned.
*/
Function cacheCheck (){
If (file_exists ($ this-> cacheFileName )){
$ CTime = $ this-> getFileCreateTime ($ this-> cacheFileName );
If ($ cTime + $ this-> cacheLimitTime> time ()){
Echo file_get_contents ($ this-> cacheFileName );
Ob_end_flush ();
Exit;
}
}
Return false;
}

/*
* Cache file or output static
* String $ staticFileName static file name (including relative path)
*/
Function caching ($ staticFileName = ""){
If ($ this-> cacheFileName ){
$ CacheContent = ob_get_contents ();
// Echo $ cacheContent;
Ob_end_flush ();

If ($ staticFileName ){
$ This-> saveFile ($ staticFileName, $ cacheContent );
}

If ($ this-> cacheLimitTime)
$ This-> saveFile ($ this-> cacheFileName, $ cacheContent );
}
}

/*
... The remaining full text>

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.