gpt4 book ai didi

PHP PDO exec() 和 commit() 有什么区别

转载 作者:行者123 更新时间:2023-12-01 10:40:24 25 4
gpt4 key购买 nike

谁能解释一下 PHP PDO 库类中 PDO::commit() 和 PDO::exec() 方法的区别。

例如**假设 PDO 连接已建立

$pdo = pdo_connection and params

$pdo->beginTransaction();

$sql = "INSERT INTO USERS VALUES ('u1','u2')";

$pdo->exec($sql);

$pdo->commit();

我想知道哪些舞台数据将永久保存在数据库中。以及 commit 和 exec 有什么区别

最佳答案

要了解发生了什么,您需要了解数据库事务

来自 Wikipedia :

A simple transaction is usually issued to the database system in a language like SQL wrapped in a transaction, using a pattern similar to the following:

  1. Begin the transaction
  2. Execute a set of data manipulations and/or queries
  3. If no errors occur then commit the transaction and end it
  4. If errors occur then rollback the transaction and end it

在您的示例代码中,您使用 beginTransaction() 开始一个新事务。

然后用 exec() 执行一个查询。在这里你可以执行更多的查询。

请注意,在此阶段,您的事务之外的任何内容都无法看到您的查询在事务中所做的任何更改。只有在提交事务之后,其他任何东西才能看到这些变化。

在事务内部时,还可以回滚,这意味着中止自事务开始以来对数据发生的任何更改。

I want to know which stage data will be saved permanently in DB.

希望从上面可以清楚地看出,只有在 commit() 调用之后,更改才会被永久保存。

关于PHP PDO exec() 和 commit() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30630329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com