这是一个使用交易的好例子吗?

时间:2018-01-14 08:27:48

标签: php mysql transactions prepared-statement

我有以下update of the users tableselection在更新后立即给我updated result,我想知道在这里使用transaction是否合适?如果是,那么我是否正确使用codewisely together with prepared statements

try {

        // connect to the database
        require 'connect.php';
        // create an update query
        $conn->beginTransaction();
        $queryUpdate = $conn->prepare("UPDATE users SET userName=:userName, firstName=:firstName,
        lastName=:lastName, password=:password, image=:image  WHERE userId=:userId"); 
        $queryUpdate->bindParam( ':userId' , $sUserId );
        $queryUpdate->bindParam( ':userName' , $sNewUserName );
        $queryUpdate->bindParam( ':firstName' , $sNewFirstName );
        $queryUpdate->bindParam( ':lastName' , $sNewLastName );
        $queryUpdate->bindParam( ':password' , $sNewPassword );
        $queryUpdate->bindParam( ':image' , $sNewImagePath );
        $bResult = $queryUpdate->execute();
        // create another query to get some of the updated values
        $querySelect = $conn->prepare("SELECT users.userName, users.firstName, users.lastName, users.image
        FROM users WHERE userId=:userId");
        $querySelect->bindParam( ':userId' , $sUserId );
        // run query 
        $querySelect->execute();  
        $ajResult = $querySelect->fetch(PDO::FETCH_ASSOC);
        // take each property one by one
        $sUserName = $ajResult['userName'];
        $sFirstName = $ajResult['firstName'];
        $sLastName = $ajResult['lastName'];
        $sImagePath = $ajResult['image'];
        // i.e. no query has failed, and we can commit the transaction
        $conn->commit();
        $sjResponse = $bResult ? '{"status":"ok", "userName":"'.$sUserName.'", "firstName":"'.$sFirstName.'",
         "lastName":"'.$sLastName.'", "image":"'.$sImagePath.'"}' : '{"status":"error"}'; 
        echo $sjResponse;
} catch (Exception $e) {
        // An exception has been thrown
        // We must rollback the transaction
        echo "ERROR";
        $conn->rollback();

}

2 个答案:

答案 0 :(得分:1)

如果您只需要传回更新结果,如果更新执行成功,则传回您刚刚在更新中使用的值。作为额外检查 - 您可以使用rowCount()检查其实际更新的内容。

require 'connect.php';
// create an update query
$queryUpdate = $conn->prepare("UPDATE users SET userName=:userName, firstName=:firstName,
lastName=:lastName, password=:password, image=:image  WHERE userId=:userId"); 
$queryUpdate->bindParam( ':userId' , $sUserId );
$queryUpdate->bindParam( ':userName' , $sNewUserName );
$queryUpdate->bindParam( ':firstName' , $sNewFirstName );
$queryUpdate->bindParam( ':lastName' , $sNewLastName );
$queryUpdate->bindParam( ':password' , $sNewPassword );
$queryUpdate->bindParam( ':image' , $sNewImagePath );
$bResult = $queryUpdate->execute();
$sjResponse = ( $bResult && $queryUpdate->rowCount() == 1) ? 
   '{"status":"ok",
    "userName":"'.$sUserName.'", 
    "firstName":"'.$sNewFirstName.'",
    "lastName":"'.$sNewLastName.'", 
    "image":"'.$sNewImagePath.'"}' 
  : '{"status":"error"}'; 
echo $sjResponse;

对于事务 - 当您对数据库进行多次更新/插入/删除时,它们更相关。因此,例如,如果您想将一些点从一个用户转移到另一个用户 - 您希望确保从用户A获取的值到达用户B.如果从A中减去该值,然后在更新用户B时失败,则积分可能会消失。使用事务,这可以回滚两个更改,一切都是一致的。

答案 1 :(得分:0)

  

我想知道在这里使用交易是否合适?

IMO,不。你只需要一个更新语句来操纵表。

因此,将多个SQL组合到单个事务中的目的也是所有相关SQL的逻辑分组类型,它们应该以相同的顺序执行。并且,您将能够使用其他功能,例如SAVEPOINTROLLBACKCOMMIT,但在执行简单查询(无交易)时可以使用其中一些功能。

我建议阅读Transactional and Locking Statements