PHP + MySQL transactions examples

后端 未结 9 2593
一整个雨季
一整个雨季 2020-11-21 11:50

I really haven\'t found normal example of PHP file where MySQL transactions are being used. Can you show me simple example of that?

And one more question. I\'ve alr

9条回答
  •  滥情空心
    2020-11-21 12:12

    I made a function to get a vector of queries and do a transaction, maybe someone will find out it useful:

    function transaction ($con, $Q){
            mysqli_query($con, "START TRANSACTION");
    
            for ($i = 0; $i < count ($Q); $i++){
                if (!mysqli_query ($con, $Q[$i])){
                    echo 'Error! Info: <' . mysqli_error ($con) . '> Query: <' . $Q[$i] . '>';
                    break;
                }   
            }
    
            if ($i == count ($Q)){
                mysqli_query($con, "COMMIT");
                return 1;
            }
            else {
                mysqli_query($con, "ROLLBACK");
                return 0;
            }
        }
    

提交回复
热议问题