In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Now is an e-commerce era, to do e-commerce must first have a mall, so the security of the mall can not be ignored, a data security mall can not be separated from the database transaction processing, the mall in capital, goods, issuing orders, checkout and other important steps plus transaction control this is not necessarily indispensable, such as some open source malls in the market I have to study.
Programmers independent development mall should also pay attention to this aspect of things, now I will introduce the next PHP operation of MySQL transactions, share with you for your reference. The specific methods are as follows:
In general, transactions should have ACID characteristics. The so-called ACID is written in the initials of Atomic (atomicity), Consistent (consistency), Isolated (isolation) and Durable (continuity). Here is an example of "bank transfer" to illustrate their meanings:
① atomicity: the statements that make up a transaction form a logical unit, and you cannot execute only part of it. In other words, a transaction is an indivisible minimum unit. For example: in the process of bank transfer, the amount of the transfer must be subtracted from one account at the same time and added to another account. It is unreasonable to change only one account.
② consistency: the database is consistent before and after the transaction is executed. That is, the transaction should transition the system state correctly. For example: in the process of bank transfer, either the amount of the transfer is transferred from one account to another, or both accounts remain the same, and there is no other situation.
③ isolation: one transaction has no effect on another. That is, it is impossible for any transaction to see a transaction in an incomplete state. For example, in the process of bank transfer, another transfer transaction can only be in a waiting state before the transfer transaction is committed.
④ persistence: the effects of transactions can be permanently preserved. On the other hand, transactions should be able to withstand all failures, including server, process, communication, media failures, and so on. For example: in the process of bank transfer, the status of the account should be saved after the transfer.
In PHP, mysqli has well encapsulated the related operations of mysql transactions. The following is an example:
$sql1 = "update User set ScoreCount = ScoreCount + 10 where ID= '123456'"
$sql2 = "update ScoreDetail set FScore = 300 where ID= '123456'"
$sql3 = "insert into ScoreDetail ID,Score) values ('123456)
$mysqli = new mysqli ('localhost','root','','DB_Lib2Test')
$mysqli- > autocommit (false); / / start things
$mysqli- > query ($sql1)
$mysqli- > query ($sql2)
If (! $mysqli- > errno) {
$mysqli- > commit ()
Echo 'ok'
} else {
Echo 'err'
$mysqli- > rollback ()
}
Here, we use the php mysql series of functions to perform the transaction.
$sql1 = "update User set ScoreCount = ScoreCount + 10 where ID= '123456'"
$sql2 = "update ScoreDetail set FScore = 300 where ID= '123456'"
$sql3 = "insert into ScoreDetail ID,Score) values ('123456)
$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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.