php数据库回滚需要设置什么意思_PHP 回滚一个事务

//to test this program u must run it twice at the same time in ie. two terminals

//this program suposedly creates a new dbase with table name NodeNames and one field in it named NodeName

//it then begins a transaction then attempts to read an element 'zTest' of field 'NodeName' which obv. doesn't exist, ignoring the returned errors

//then it writes it(since it wasn't there)

//then decides to rollBack the transaction and eventually try a new one

//because rollBack doesn't really work(apparently) for some unknown reason, beginTransaction fails saying 'There is already an active transaction'$db= newPDO('sqlite:demlinks6.3sql',''/*user*/,''/*pwd*/);$db->exec('CREATE TABLE \'NodeNames\' ("NodeName" VARCHAR(10));');$db->beginTransaction();$getter="zTest";$pgn=$db->prepare('SELECT * FROM \'NodeNames\' WHERE "NodeName" = :node13');$pgn->bindParam(":node13",$getter,PDO::PARAM_STR);//read$pgn->execute();//execute above SELECT$ar=$pgn->FetchAll();//get array of results$writter="zTest";$pnn=$db->prepare('INSERT INTO \'NodeNames\' ("NodeName") VALUES (:node14)');$pnn->bindParam(":node14",$writter,PDO::PARAM_STR);//write$pnn->execute();//write it!echo"waiting...";usleep(2000000);

echo"done\n";$db->rollBack();//this doesn't do it's job$db->beginTransaction();//here it fails, when running this program twice at the same time; 'There is already an active transaction'

//unreachable:$db->commit();?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP中,可以使用mysqli或PDO扩展来操作MySQL数据库。无论使用哪种扩展,回滚操作都是类似的。 回滚操作通常用于撤销之前的事务,以确保数据的完整性。例如,当插入或更新多个表时,如果其中一个操作失败,您可能需要回滚整个事务,以确保所有表都保持一致状态。 下面是一个简单的示例,演示如何在PHP中执行回滚操作: 使用mysqli扩展: ``` // 连接到数据库 $conn = mysqli_connect("localhost", "username", "password", "dbname"); // 开始事务 mysqli_begin_transaction($conn); try { // 执行一些数据库操作 // 如果操作失败,则回滚事务 if (some_condition) { mysqli_rollback($conn); } // 提交事务 mysqli_commit($conn); } catch (Exception $e) { // 发生异常时回滚事务 mysqli_rollback($conn); } // 关闭数据库连接 mysqli_close($conn); ``` 使用PDO扩展: ``` // 连接到数据库 $conn = new PDO("mysql:host=localhost;dbname=db", "username", "password"); // 开始事务 $conn->beginTransaction(); try { // 执行一些数据库操作 // 如果操作失败,则回滚事务 if (some_condition) { $conn->rollBack(); } // 提交事务 $conn->commit(); } catch (Exception $e) { // 发生异常时回滚事务 $conn->rollBack(); } // 关闭数据库连接 $conn = null; ``` 无论使用mysqli还是PDO扩展,回滚操作都需要事务中执行。在示例中,我们使用了try-catch块来捕获异常并回滚事务。如果没有发生异常,则提交事务。如果有任何操作失败,则回滚事务。最后,我们关闭数据库连接。 请注意,回滚操作只能在事务中执行。如果您没有使用事务,则无法执行回滚操作。因此,在执行任何数据库操作之前,请确保已启用事务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值