php - 如何正确执行交易?
问题描述
我对 php/mysqli 编码并不陌生,但我以前从未真正使用过事务。我发现有关如何正确启动和执行事务的信息相互矛盾。我见过一些人使用autocommit(false)
,其他人只使用begin_transaction();
,其他人说只在运行检查后提交,但其他人说如果有错误提交不会运行......我真的很困惑什么是正确的语法是。任何帮助表示赞赏!
//turn autocommit off and start transaction
$mysqli->autocommit(false);
$mysqli->begin_transaction();
//declare queries and make sure they all run properly
$result1 = $mysqli->query(myqueryhere);
if(!$result1) { $error[] = true; }
$result2 = $mysqli->query(mysecondqueryhere);
if(!$result2) { $error[] = true; }
$result3 = $mysqli->query(mythirdqueryhere);
if(!$result3) { $error[] = true; }
//only commit if there are no errors
if(empty($error)) {
$mysqli->commit();
}
//turn autocommit back on for future queries
$mysqli->autocommit(true);
解决方案
推荐阅读
- javascript - 如何为重复模式创建正则表达式?
- reactjs - 在 MUI 自动完成中删除鼠标离开时的突出显示
- java - 从代码中导航到源位置
- javascript - 未捕获的类型错误:无法读取未定义的属性(读取“角色”)
- eslint - 在单行 if 语句的右括号之后强制使用空格的 eslint 规则是什么?
- python - ValueError: Worksheet index 0 is invalid, 0 worksheets found.Cannot open xlsx with pandas in python
- sql-server - How to split a string from a table SQL Server 2012?
- kubernetes - 使用 Kubernetes 和 CRDs 作为数据库,然后搜索
- javascript - 获取选定的变化价格和 sku 并将其显示到 woocommerce 中的单个产品
- mysql - 使用命名空间 sql 的 omnet 问题的 sEden 框架失败