duaeim2874 2019-02-27 13:21
浏览 116

在try catch中使用mysqli prepared语句来处理程序php中的事务

I always used PHP in procedural way using mysqli. Now I am working with interdependent queries so I am using transactions (if query1 ok then proceed with query2 and commit, if not rollback). I always used if/else to check the flow/errors, but now I have something more articulated so I would like to use try/catch, because I would not like to have to do thousands IF to check every single query.
Given this code (just an example):

<?php

try {
    // disable autocommit
    mysqli_autocommit( $db, FALSE );

    // lock tables
    $query = 'LOCK TABLES `table` WRITE, `other_table` WRITE';

    mysqli_query( $db, $query );

    // QUERY 1:
    $query = 'SELECT `field` FROM `table` WHERE `id` = ?';

    $stmt = mysqli_prepare( $db, $query );

    if ( $stmt ) {
        // bind parameters
        mysqli_stmt_bind_param( $stmt, 'i', $_POST[ 'id' ] );

        // execute query
        mysqli_stmt_execute( $stmt );

        // bind result
        mysqli_stmt_bind_result( $stmt, $stmt_result );

    }

    // QUERY 2 INTERDEPENDENT FROM QUERY 1:
    $query = 'DELETE FROM `other_table` WHERE `id` = ?';

    $stmt = mysqli_prepare( $db, $query );

    if ( $stmt ) {
        // bind parameters
        mysqli_stmt_bind_param( $stmt, 'i', $stmt_result );

        // execute query
        mysqli_stmt_execute( $stmt );
    }

    // unlock tables
    $query = 'UNLOCK TABLES';

    mysqli_query( $db, $query );

    ///////////////////////////////////////////////////////////////
    // //////// IF SCRIPT ARRIVES HERE IT MEANS EVERYTHING IS OK:
    ///////////////////////////////////////////////////////////////

    // Commit
    mysqli_commit( $db );

    // re-enable autocommit
    mysqli_autocommit( $db, TRUE );

}
///////////////////////////////////////////////////////////////
// //////// IF ANYTHING GIVES ERROR/THROW EXCEPTION HANDLE IT:
///////////////////////////////////////////////////////////////
catch ( Exception $e ) {
    // rollback
    mysqli_rollback( $db );

    // re-enable autocommit
    mysqli_autocommit( $db, TRUE );

    // show error
    $_SESSION[ 'alert' ] = "ERROR";
    $_SESSION[ 'messaggio' ] = "Error: " . $e->getMessage();
    mysqli_close( $db );
    header( "Location: output.php" );
    exit();
}

?>

My questions are:
1- is it possible (and correct) to use procedural mysqli prepared statements in a try/catch?
2- is ok to proceed that way? I mean keep all the queries of the transaction and theirs execution in a TRY, so all the errors are catched by its own CATCH?
3- is it ok to use always the same variable $stmt like I wrote in the code or do I need to close it every time with mysqli_stmt_close($stmt) before proceed with next prepare?
4- where i wrote comment:
// IF SCRIPT ARRIVES HERE IT MEANS EVERYTHING IS OK:
am I right or I THINK to be right?

I hope all is clear and to receive several answers because apart of the code, I am not sure if the way to proceed is correct, I would not like to have to do thousands IF to check every single query, I am sure there is a better way.
Thanks everyone in advance if you want to give me a feedback

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?