mysql表中的多次插入

发布于 2024-10-31 09:17:58 字数 99 浏览 8 评论 0原文

我在插入 mysql 表时遇到问题。我有 3 个相关表要在保存时插入,但我想确保要么运行所有查询,要么都不运行。我已经在 C# 中完成了此操作,但我想知道如何在 PHP 中完成此操作。

i am facing a problem in inserting into mysql tables. I have 3 related tables to insert on save, but I want to make sure that either all of the queries are run, or none of them. I have done this in C#, but I want to know how i can do it in PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-11-07 09:17:58

为了实现全有或全无的行为,您需要使用事务:

  • 则启动事务
  • 执行查询
  • 如果所有查询均已成功执行,
  • ,否则提交,回滚。

In PHP, if working with [**PDO**][1], you'll want to use the [**`PDO::beginTransaction()`**][2], [**`PDO::commit()`**][3] and [**`PDO::rollback()`**][4] methods.

如果使用MySQLi,您将需要使用mysqli::commit()mysqli::rollback() -- 使用 禁用自动提交后>mysqli::autocommit()

To detect if a query has failed, you'll want to :

In order to have a all or nothing behavior, you'll want to use a transaction :

  • start the transaction
  • do your queries
  • if all queries have been executed successfully, commit
  • else, rollback.

In PHP, if working with [**PDO**][1], you'll want to use the [**`PDO::beginTransaction()`**][2], [**`PDO::commit()`**][3] and [**`PDO::rollback()`**][4] methods.

If working with MySQLi, you'll want to use mysqli::commit() and mysqli::rollback() -- after having disabled autocommit with mysqli::autocommit().

To detect if a query has failed, you'll want to :

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文