这个PDO交易有什么问题

时间:2016-02-10 09:15:36

标签: php mysql pdo

我的代码中总共有4个查询。我正在使用PDO交易 在前3个查询。它正确执行代码,但在第四个查询中它给出了一个错误:

  

发生错误:没有活动的交易。

它的解决方案是什么?

为什么3个查询提交对数据库的更改,即使它给第4个查询错误。

这是代码:

<?php 
session_start();
    require_once 'dbconfig.php';
    try {

            $conn = new PDO("mysql:host=$host;dbname=$dbname",$username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
    $temp = "123456";
    $temp1 = "LHPL002";
    $temp2 = "Declined";
    $temp3 = "This is the reason";
    $conn->beginTransaction();

    $stmt = $conn->prepare("UPDATE tble_transaction SET  signed=? WHERE tracknum= ?");
                $stmt->bindParam(1, $temp2, PDO::PARAM_STR, 30);
                $stmt->bindParam(2, $temp, PDO::PARAM_STR, 30);
                $stmt->execute();
    if ($stmt->rowCount() > 0){

        echo "good";
                $stmt = $conn->prepare("select  tracknum,   login_id from tble_document where tracknum= ?");
                $stmt->bindParam(1, $temp, PDO::PARAM_STR, 30);
                $stmt->execute();
                if ($stmt->rowCount() > 0){
                    $row = $stmt->fetch(PDO::FETCH_ASSOC);
                     $logid =  $row['login_id'];
                     echo $logid;
                     echo "good 1";
                     $conn->commit();

                            $stmt = $conn->prepare("INSERT INTO tble_declined ( tracknum, signatoryid,  reason,  status, login_id) VALUES (?,?, ?, ?, ?)");
                            $stmt->bindParam(1, $temp, PDO::PARAM_STR, 30);
                            $stmt->bindParam(2, $temp1, PDO::PARAM_STR, 30);
                            $stmt->bindParam(3, $temp3, PDO::PARAM_STR, 30);
                            $stmt->bindParam(4, $temp2, PDO::PARAM_STR, 30);
                            $stmt->bindParam(5, $logid, PDO::PARAM_STR, 30);
                            $stmt->execute();
                            if ($stmt->rowCount() > 0){
                                 echo "good 2";
                                 $conn->commit();

                                        $stmt = $conn->prepare("select signatorycontact from tble_signatory where login_id= ?");
                                        $stmt->bindParam(1, $logid, PDO::PARAM_STR, 30);
                                        $stmt->execute();
                                        if ($stmt->rowCount() > 0){
                                            echo "good 3";
                                            $row = $stmt->fetch(PDO::FETCH_ASSOC);
                                            echo  $row['signatorycontact'];

                                             $conn->commit();
                                        }else {

                                        $conn->rollBack();
                                        }
                            }else {

                            $conn->rollBack();
                            }
                }else {

                $conn->rollBack();
                }
    }else  {

            $conn->rollBack();
        }
    } catch (PDOException $pe) {
        die("Error occurred:" . $pe->getMessage());
    }   

?>

1 个答案:

答案 0 :(得分:1)

您开始一次交易

$temp3 = "This is the reason";
$conn->beginTransaction();

然后,在两次查询后提交事务

echo "good 1";
$conn->commit();

然后你也执行一个查询并尝试提交它们

echo "good 2";
$conn->commit();

但是您没有活动事务,因为您提交了上面的事务(在第一次提交中)。

Here您可以找到有关嵌套事务的评论。我认为这就是你所需要的。