php事务回滚不了的解决办法:1、打开相应的php文件;2、检查“function update_user_set_date_of_birth($date_of_birth){...}”代码;3、通过“$this->mysqli->autocommit(false);”方式关闭自动提交即可。
本教程操作环境:windows7系统、php8.1版、dell g3电脑。
php事务回滚不了怎么办?
问题描述:
关于php 事务未执行回滚
mysqliphptransactionstransactions not doing the rollback
我具有更新用户的功能:
function update_user_set_common($date_of_birth, $mobile, $street, $housenumber, $addition, $postal_code, $location, $country){ try { $this->mysqli->begin_transaction(); $this->update_user_set_date_of_birth($date_of_birth); $this->update_user_set_mobile($mobile); $this->update_user_set_address($street, $housenumber, $addition, $postal_code, $location, $country); $this->mysqli->commit(); return true; } catch (exception $e) { $this->mysqli->rollback(); echo"error:" . $e; return false; }}
如您所见,我进行了一笔交易。如果一切正常,则应提交,如果发生错误,则应回滚。
这是我的更新查询方式的一个示例:
function update_user_set_date_of_birth($date_of_birth){ return $this->update_user_set_singlefield(col_user_date_of_birth, $date_of_birth);}function update_user_set_singlefield($field, $value){ if ($update_stmt = $this->mysqli->prepare("update" . table_user ." set" . $field ." = ? where" . col_user_id ." = ?")) : if (!$update_stmt->bind_param("ss", $value, $this->user_id)) : $update_stmt->close(); return false; endif; if (!$update_stmt->execute()) : $update_stmt->close(); return false; else : $update_stmt->close(); return true; endif; else : return false; endif;}
因此现在例如update_user_set_mobile失败,但是没有回滚。 update_user_set_date_of_birth之前的语句仍然会执行,并且不会变回原先。
为什么没有回滚?
示例创建表以显示我使用innodb:
create table `user` ( `user_id` bigint(20) not null primary key auto_increment, `user_e_mail` varchar(255) collate utf8_bin not null, `user_passwort` varchar(255) collate utf8_bin not null, `user_passwort_salt` varchar(255) collate utf8_bin not null, `user_firstname` varchar(255) collate utf8_bin not null, `user_lastname` varchar(255) collate utf8_bin not null, `user_date_of_birth` varchar(200) collate utf8_bin default null, `user_mobile` varchar(255) collate utf8_bin default null, `address_id` bigint(20) default null) engine=innodb default charset=utf8 collate=utf8_bin;
问题解决:
关闭自动提交而不是mysqli-> begin_transaction();
$this->mysqli->autocommit(false);
http://php.net/manual/zh/mysqli.autocommit.php
推荐学习:《php视频教程》
以上就是php事务回滚不了怎么办的详细内容。