【发布时间】:2018-10-09 14:02:42
【问题描述】:
我使用 pdo 创建了一个 mysql 事务。
请检查我的代码是否完美。如果它不正确,那么我如何正确设置交易。
我需要使用 try 块吗?我如何在这个查询中对 try 块进行排序
if(is_null($this->pdo)){
return false;
} else {
$pdo = $this->pdo;
$pdo->beginTransaction();
$stmt = $pdo->prepare("SELECT mallu FROM users WHERE id=? LOCK IN SHARE MODE");
$stmt->execute([$alpharef]);
$reseller = $stmt->fetch();
$stmt = null;
$stmt = $pdo->prepare("UPDATE users SET mallu = mallu - :cost WHERE id=:userid");
$stmt->bindParam(':cost', $cost, PDO::PARAM_STR);
$stmt->bindParam(':userid', $alpharef, PDO::PARAM_INT);
if (stmt->execute() != true) {
$conn->rollBack();
return false
}
$stmt = null;
$dates = date("d-m-Y");
$statos = 0;
$stmt = $pdo->prepare("INSERT INTO request (rtyp, num, amount, cost, typ, usr, refid, tm, dates, status) VALUES (:rtyp, :num, :amount, :cost, :typ, :usr, :refid, NOW(), :dates, :status)");
$stmt->bindParam(':rtyp', $retyp, PDO::PARAM_STR);
............................................................
if (stmt->execute() != true) {
$conn->rollBack();
return false
}
$stmt = null;
$pdo->commit();
$this->msg = 'Request sent successfully';
return true;
}
$pdo = null;
【问题讨论】:
标签: pdo transactions