在 PDO 中哪里可以回滚事务?
我的问题是,我通过此链接进行了数据库设计 我的数据库是否过度设计?
编辑* 好的也许使用交易?但是如果失败我应该把回滚放在哪里?
$dbConnect->beginTransaction();
$RegisterInsert = $dbConnect->prepare("INSERT INTO companies (
`name`, `address`, `email`, `phone`, `link`, `verified`) VALUES (
:name, :address, :email, :phone, :link, :verified)");
$RegisterInsert->execute($RegisterData);
$RegisterData2['CID'] = $dbConnect->lastInsertId();
$RegisterInsert = $dbConnect->prepare("INSERT INTO users_companies (
`UID`, `CID`, `role`) VALUES (
:UID, :CID, :role)");
$RegisterInsert->execute($RegisterData2);
$dbConnect->commit();
我应该把回滚放在哪里?
谢谢
my problem is , i have a database design from this link is my database overdesigned?
edit* ok maybe useing transaction ? but where should i put the rollback if it fails ?
$dbConnect->beginTransaction();
$RegisterInsert = $dbConnect->prepare("INSERT INTO companies (
`name`, `address`, `email`, `phone`, `link`, `verified`) VALUES (
:name, :address, :email, :phone, :link, :verified)");
$RegisterInsert->execute($RegisterData);
$RegisterData2['CID'] = $dbConnect->lastInsertId();
$RegisterInsert = $dbConnect->prepare("INSERT INTO users_companies (
`UID`, `CID`, `role`) VALUES (
:UID, :CID, :role)");
$RegisterInsert->execute($RegisterData2);
$dbConnect->commit();
where should i put the rollback ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事务应该以
rollback()
或commit()
结束,(仅其中之一)它通常与
if...else< 一起使用/code> 语句逻辑上只应执行其中之一。
当查询涉及复杂逻辑时,通常会使用事务。
如果您使用 MySQL,请确保您没有对表使用 MyISAM 引擎,因为它不支持事务。
A transaction should end with either a
rollback()
or acommit()
, (only one of them)Its usually used with an
if...else
statement as logically only one of them should be executed.Transactions are usually used when there is a complex logic involved with queries.
In case you are using MySQL, make sure you are not using MyISAM engine for tables, as it doesn't support transactions.
一旦您知道整个事务将失败,那么您应该回滚到目前为止所做的事情,而不是尝试任何进一步的更新 - 所以在伪代码中:
HTH
C。
As soon as you know that the transaction as a whole is going to fail then you should rollback what you've done so far and not try any further updates - so in pseudo-code:
HTH
C.