gpt4 book ai didi

PHP PDO MySQL 事务代码结构

转载 作者:可可西里 更新时间:2023-11-01 06:34:21 24 4
gpt4 key购买 nike

我正在尝试使用 PHP/PDO 在 MySQL 中设置我的第一个事务...

我有一个简单的问题,确定上一个查询是否成功的最佳方法是什么?这是我现在拥有的,但我更愿意找到一种使用 if 语句测试查询的方法。

这几乎是模拟代码,试图获得一个工作模型。我知道 $results 不能有效地测试任何东西是好是坏。我在那里更多地作为真正交易的占位符时间到了..

if ($_POST['groupID'] && is_numeric($_POST['groupID'])) {
$sql = "SET AUTOCOMMIT=0";
$dbs = $dbo->prepare($sql);
$dbs->execute();

$sql = "START TRANSACTION";
$dbs = $dbo->prepare($sql);
$dbs->execute();

$sql = "DELETE FROM users_priveleges WHERE GroupID=:groupID";
$dbs = $dbo->prepare($sql);
$dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT);
$dbs->execute();

try {
$sql = "DELETE FROM groups WHERE GroupID=:groupID LIMIT 1";
$dbs = $dbo->prepare($sql);
$dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT);
$dbs->execute();

$results["error"] = null;
$results["success"] = true;

try {
$sql = "DELETE FROM users WHERE Group=:groupID";
$dbs = $dbo->prepare($sql);
$dbs->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT);
$dbs->execute();

$results["error"] = null;
$results["success"] = true;

$sql = "COMMIT";
$dbs = $dbo->prepare($sql);
$dbs->execute();
}
catch (PDOException $e) {
$sql = "ROLLBACK";
$dbs = $dbo->prepare($sql);
$dbs->execute();

$results["error"] = "Could not delete associated users! $e";
$results["success"] = false;
}
}
catch (PDOException $e)
{
$sql = "ROLLBACK";
$dbs = $dbo->prepare($sql);
$dbs->execute();

$results["error"] = "COULD NOT REMOVE GROUP! $e";
$results["success"] = false;
}
}

最佳答案

一些一般注意事项:不要使用 bindParam() 除非您使用修改参数值的过程因此,使用 bindValue()。 bindParam() 接受参数值作为引用变量。这意味着您不能执行 $stmt->bindParam(':num', 1, PDO::PARAM_INT); - 它会引发错误。此外,PDO 有自己的功能来控制事务,您不需要手动执行查询。

我稍微重写了您的代码以阐明如何使用 PDO:

if($_POST['groupID'] && is_numeric($_POST['groupID']))
{
// List the SQL strings that you want to use
$sql['privileges'] = "DELETE FROM users_priveleges WHERE GroupID=:groupID";
$sql['groups'] = "DELETE FROM groups WHERE GroupID=:groupID"; // You don't need LIMIT 1, GroupID should be unique (primary) so it's controlled by the DB
$sql['users'] = "DELETE FROM users WHERE Group=:groupID";

// Start the transaction. PDO turns autocommit mode off depending on the driver, you don't need to implicitly say you want it off
$pdo->beginTransaction();

try
{
// Prepare the statements
foreach($sql as $stmt_name => &$sql_command)
{
$stmt[$stmt_name] = $pdo->prepare($sql_command);
}

// Delete the privileges
$stmt['privileges']->bindValue(':groupID', $_POST['groupID'], PDO::PARAM_INT);
$stmt['privileges']->execute();

// Delete the group
$stmt['groups']->bindValue(":groupID", $_POST['groupID'], PDO::PARAM_INT);
$stmt['groups']->execute();

// Delete the user
$stmt['users']->bindParam(":groupID", $_POST['groupID'], PDO::PARAM_INT);
$stmt['users']->execute();

$pdo->commit();
}
catch(PDOException $e)
{
$pdo->rollBack();

// Report errors
}
}

关于PHP PDO MySQL 事务代码结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618618/

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