The PHP timestamp uses the instance code. We will surely encounter this situation: Bank A and Bank B open your account almost simultaneously and see the original 1000 yuan deposit in your account, then both banks want to add 5 to your account. we will certainly encounter this situation: bank A and Bank B open your account almost simultaneously and see the original 1000 yuan deposit in your account. then both banks want to add 500 yuan in deposits to your account. Then, Bank A will change 1000 yuan to 1500 yuan, and Bank B will also change 1000 yuan to 1500 yuan. This is terrible! In the end, your bank account has only 1500 yuan instead of 2000 yuan, which is equivalent to a loss of 500 yuan! This is a serious problem caused by modification without locking the data. However, we can use timestamps to solve this problem.
Let's look at the ideas:
Create a timestamp field in the bank account table and set it to the text type varchar.
When Bank A reads the deposit field in the account table, it also reads the timestamp field, such as 123456.
When bank A modifies the deposit value and saves the disk, it compares the previously read timestamp 123456 with the timestamp in the current table. if the timestamp is the same, it is allowed to store the disk, then generate a new timestamp, such as 456789, to replace the original timestamp 123456 in the table.
What are the benefits of doing so.
Let's take A look at the first situation: Bank A and Bank B opened your account almost simultaneously and saw the original 1000 yuan deposit in your account, at the same time, the two banks read A timestamp of 123456 at the same time, and then there is A difference. when bank A Changes 1000 yuan to 1500 yuan, the system will compare whether the previous timestamp 123456 is consistent with the timestamp in the table when saving the disk. Obviously, it should be consistent now, so the disk can be stored, the new timestamp 456789 is generated to replace the old timestamp 123456. Next, Bank B also changed the 1000 yuan to 1500 yuan for disk storage. The system compares the previous timestamp 123456 with the timestamp in the table during disk storage, it is found that the previous timestamp 123456 is different from the current timestamp 456789. The system rejects disk storage and requires data refresh, then, after the data is refreshed, the RMB 1000 has already been converted into RMB 500 because bank A has previously saved RMB 1500, so bank B will replace it with RMB 1500 on the basis of RMB 2000 and store it again, the system allows. In this way, we can avoid errors caused by repeated data modification!
I hope you can understand what I mean ~
Finally, let's take a look at some of the time stamp operation code in PHP.
Obtain timestamp
$ Timestamp = time ();
Echo $ timestamp;
?>
SQL statement for updating timestamp:
Update table name set field name = $ timestamp where Condition = value;
By Sunec
Original load: Cenus Blog
Copyright. The author and original source and this statement must be indicated in the form of links during reprinting.
...