The relationship between try catch in php and transactions in mysql
伊谢尔伦
伊谢尔伦 2017-06-07 09:23:24
0
3
1037

With try catch, do I no longer need to use mysql transactions?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
三叔

See if your try catch operates on the database. Of course it involves updating and inserting multiple tables. In order to ensure the integrity of the data, transaction operations must be enabled

//開啟事務
try {
//完成提交事務
} catch(throw $e) {
//rollback
}
给我你的怀抱

It depends on what is in your catch. If there is only one sentence after try and an exception is thrown, there will be no commit, OK.
If doOne is executed and you don’t use the transaction, it will autocommit. Do you want to catch doOne and roll it back separately or what to do?
The following are two sentences. If you have 10 sentences, how will you catch them?

try {
 doOne
 doTwo
} catch(throw $e) {
//rollback
}
Peter_Zhu

Transaction and try catch are two different things. The reason why try catch is used when starting a transaction is because if you use extensions such as pdo mysqli, the database error will return an error to php. , let the program executed by PHP terminate, so that the function command of the rollback step cannot be executed (the program is interrupted when the input is executed), and it is much safer to use try catch to perform transaction operations

It is necessary to distinguish clearly try catch is something that belongs to php java and other logical operations, while transation is something that belongs to the database. The two can cooperate but cannot replace each other

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template