PHP operation MySQL Transaction instance, MySQL transaction instance _php tutorial

Source: Internet
Author: User
Tags php mysql php programming

PHP operation MySQL Transaction instance, MySQL transaction instance


This article describes the PHP operation of the MySQL transaction methods, share to everyone for your reference. Here's how:

In general, transactions should have acid characteristics. The so-called acid is Atomic (atomicity), consistent (consistency), Isolated (isolation), durable (persistent) four words written in the first letter, the following "bank transfer" as an example to explain their meaning:

① atomicity: The statement that makes up the transaction forms a logical unit and cannot execute only part of it. In other words, a transaction is the smallest unit that is indivisible. For example, in a bank transfer process, you must subtract the transfer amount from one account and add it to another account, it is unreasonable to change only one account.
② Consistency: The database is consistent before and after the execution of the transaction. In other words, the transaction should correctly convert the System state. For example: During a bank transfer, the transfer amount is transferred from one account to another, or two accounts are not changed, and there is no other situation.
③ Isolation: One transaction has no effect on another transaction processing. This means that no transaction is likely to see a transaction in an incomplete state. For example, in a bank transfer process, another transfer transaction can only be in the waiting state until the transfer transaction is not committed.
④ Persistence: The effect of transaction processing can be permanently preserved. Conversely, transactions should be able to withstand all failures, including servers, processes, communications, media failures, and so on. For example: In the process of bank transfer, the status of transfer payback can be saved.

In PHP, MYSQLI has already encapsulated the operation of MySQL transactions well. The following example:
Copy CodeThe code is as follows: $sql 1 = "Update User set scorecount = Scorecount +10 where id= ' 123456 '";
$sql 2 = "Update scoredetail set fscore =" where id= ' 123456 ' ";
$sql 3 = "INSERT INTO Scoredetail id,score" values (' 123456 ', 60) ";
$mysqli = new mysqli (' localhost ', ' root ', ' ', ' db_lib2test ');
$mysqli->autocommit (FALSE);//Start things
$mysqli->query ($sql 1);
$mysqli->query ($sql 2);
if (! $mysqli->errno) {
$mysqli->commit ();
echo ' OK ';
}else{
echo ' err ';
$mysqli->rollback ();
}
Here, we then use the PHP MySQL series function to perform the transaction.
Copy CodeThe code is as follows: $sql 1 = "Update User set scorecount = Scorecount +10 where id= ' 123456 '";
$sql 2 = "Update scoredetail set fscore =" where id= ' 123456 ' ";
$sql 3 = "INSERT INTO Scoredetail id,score" values (' 123456 ', 60) ";
$conn = mysql_connect (' localhost ', ' root ', ');
mysql_select_db (' db_lib2test ');
mysql_query (' Start transaction ');
mysql_query (' SET autocommit=0 ');
mysql_query ($sql 1);
mysql_query ($sql 2);
if (Mysql_errno ()) {
mysql_query (' rollback ');
echo ' err ';
}else{
mysql_query (' commit ');
echo ' OK ';
}
mysql_query (' SET autocommit=1 ');
mysql_query ($sql 3);

Here to note:

MyISAM: Transactions are not supported for read-only programs to improve performance
InnoDB: Supports acid transactions, row-level locks, concurrency
Berkeley DB: Support transactions

It is hoped that this article is helpful to everyone's Php+mysql database program design.


Who gives a PHP operation MySQL class and has detailed usage instructions or examples

below this, is a simple database encapsulation class for PHP5, suitable for learning, and other actions such as delete, update, you can add yourself:
!--? php
class mysql{//First define a class, Uppercase letters
Public $host;//server name, access modifier publicly proves that $host is a common condition that is accessible inside and outside the class, can be inherited
public $user;//user name, is a common property
private $pass;//password, ask modifier private certificate $pass is private. Can only be used inside a class and cannot be inherited.
public $dbname;//database name, which is also a common property.
//__construct Reputation This is a constructor 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 lookup and display functions for the database
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 '

'. Htmlspecialchars (Stripslashes ($row [' body ']). "
</td></tr> ";
}
}
}
$rutt = new Mysql (' localhost ', ' root ', ' ssss ', ' Calvin ');//Instantiate a class ... Remember that the parameters here are the same as the parameters of the constructor ...
$rutt->myquery (...) Remaining full text >>

Want to make a website, a PHP programming code, PHP MySQL cache how to implement? It's best to give an example

The following are the cache classes:
Class Cache {
Cache Directory
var $cacheRoot = "./cache/";
Cache update time seconds, 0 for no cache
var $cacheLimitTime = 0;
Cache file Name
var $cacheFileName = "";
Cache extension
var $cacheFileExt = "html";

/*
* Constructor function
* int $cacheLimitTime Cache update Time
*/
function Cache ($cacheLimitTime) {
if (Intval ($cacheLimitTime))
$this->cachelimittime = $cacheLimitTime;
$this->cachefilename = $this->getcachefilename ();
Echo $this->cachefilename;
Ob_start ();
}

/*
* Check if the cache file is within the set update time
* Return: If the contents of the file are returned within the update time, the reverse returns the failure
*/
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 files or output static
* String $staticFileName static file name (with 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);
}
}

/*
...... Remaining full text >>

http://www.bkjia.com/PHPjc/906673.html www.bkjia.com true http://www.bkjia.com/PHPjc/906673.html techarticle PHP operation MySQL Transaction instance, MySQL transaction instance This article describes the PHP operation MySQL transaction methods, share for everyone to reference. Here's how: in general, business should ...

  • 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.