我真的没有发现正常的PHP文件的例子,MySQL事务正在使用。你能举个简单的例子吗?

还有一个问题。我已经做过很多编程,没有使用过事务。我能不能在header。PHP中放一个PHP函数,如果一个mysql_query失败了,那么其他的也会失败?


我想我已经弄明白了,对吗?:

mysql_query("SET AUTOCOMMIT=0");
mysql_query("START TRANSACTION");

$a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
$a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");

if ($a1 and $a2) {
    mysql_query("COMMIT");
} else {        
    mysql_query("ROLLBACK");
}

当前回答

我在处理事务时通常使用的思想是这样的(半伪代码):

try {
    // First of all, let's begin a transaction
    $db->beginTransaction();
    
    // A set of queries; if one fails, an exception should be thrown
    $db->query('first query');
    $db->query('second query');
    $db->query('third query');
    
    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed, and we can commit the transaction
    $db->commit();
} catch (\Throwable $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $db->rollback();
    throw $e; // but the error must be handled anyway
}

Note that, with this idea, if a query fails, an Exception must be thrown:

PDO可以做到这一点,这取决于您如何配置它 看到PDO:: setAttribute 和PDO::ATTR_ERRMODE和PDO::ERRMODE_EXCEPTION 否则,使用一些其他API,您可能必须测试用于执行查询的函数的结果,并自己抛出异常。


Unfortunately, there is no magic involved. You cannot just put an instruction somewhere and have transactions done automatically: you still have to specific which group of queries must be executed in a transaction.

例如,您经常会在事务之前(开始之前)有几个查询,在事务之后(提交或回滚之后)有另外两个查询,您希望无论事务中发生了什么(或没有发生什么),都能执行这些查询。

其他回答

使用PDO连接时:

$pdo = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', $user, $pass, [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // this is important
]);

我经常使用以下代码进行事务管理:

function transaction(Closure $callback)
{
    global $pdo; // let's assume our PDO connection is in a global var

    // start the transaction outside of the try block, because
    // you don't want to rollback a transaction that failed to start
    $pdo->beginTransaction(); 
    try
    {
        $callback();
        $pdo->commit(); 
    }
    catch (Exception $e) // it's better to replace this with Throwable on PHP 7+
    {
        $pdo->rollBack();
        throw $e; // we still have to complain about the exception
    }
}

使用的例子:

transaction(function()
{
    global $pdo;

    $pdo->query('first query');
    $pdo->query('second query');
    $pdo->query('third query');
});

这样,事务管理代码就不会在整个项目中重复。这是一件好事,因为从这篇文章中其他与pdo相关的回答来看,它很容易犯错误。最常见的错误是忘记重新抛出异常,并在try块中启动事务。

另一个mysqli_multi_query的过程式示例,假设$query由分号分隔的语句填充。

mysqli_begin_transaction ($link);

for (mysqli_multi_query ($link, $query);
    mysqli_more_results ($link);
    mysqli_next_result ($link) );

! mysqli_errno ($link) ?
    mysqli_commit ($link) : mysqli_rollback ($link);

我有这个,但不确定这是否正确。也可以试试这个。

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");
}

创意来自这里:http://www.phpknowhow.com/mysql/transactions/

我在处理事务时通常使用的思想是这样的(半伪代码):

try {
    // First of all, let's begin a transaction
    $db->beginTransaction();
    
    // A set of queries; if one fails, an exception should be thrown
    $db->query('first query');
    $db->query('second query');
    $db->query('third query');
    
    // If we arrive here, it means that no exception was thrown
    // i.e. no query has failed, and we can commit the transaction
    $db->commit();
} catch (\Throwable $e) {
    // An exception has been thrown
    // We must rollback the transaction
    $db->rollback();
    throw $e; // but the error must be handled anyway
}

Note that, with this idea, if a query fails, an Exception must be thrown:

PDO可以做到这一点,这取决于您如何配置它 看到PDO:: setAttribute 和PDO::ATTR_ERRMODE和PDO::ERRMODE_EXCEPTION 否则,使用一些其他API,您可能必须测试用于执行查询的函数的结果,并自己抛出异常。


Unfortunately, there is no magic involved. You cannot just put an instruction somewhere and have transactions done automatically: you still have to specific which group of queries must be executed in a transaction.

例如,您经常会在事务之前(开始之前)有几个查询,在事务之后(提交或回滚之后)有另外两个查询,您希望无论事务中发生了什么(或没有发生什么),都能执行这些查询。

我想我已经弄明白了,对吗?:

mysql_query("START TRANSACTION");

$a1 = mysql_query("INSERT INTO rarara (l_id) VALUES('1')");
$a2 = mysql_query("INSERT INTO rarara (l_id) VALUES('2')");

if ($a1 and $a2) {
    mysql_query("COMMIT");
} else {        
    mysql_query("ROLLBACK");
}