PHP + MySQL transactions examples

后端 未结 9 2666
一整个雨季
一整个雨季 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:04

    I had this, but not sure if this is correct. Could try this out also.

    mysql_query("START TRANSACTION");
    $flag = true;
    $query = "INSERT INTO testing (myid) VALUES ('test')";
    
    $query2 = "INSERT INTO testing2 (myid2) VALUES ('test2')";
    
    $result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR);
    if (!$result) {
    $flag = false;
    }
    
    $result = mysql_query($query2) or trigger_error(mysql_error(), E_USER_ERROR);
    if (!$result) {
    $flag = false;
    }
    
    if ($flag) {
    mysql_query("COMMIT");
    } else {        
    mysql_query("ROLLBACK");
    }
    

    Idea from here: http://www.phpknowhow.com/mysql/transactions/

提交回复
热议问题