【问题标题】:PHP & mySQL: Simple code to implement Transaction - Commit & RollbackPHP & mySQL:实现事务的简单代码 - 提交和回滚
【发布时间】:2011-01-09 03:22:25
【问题描述】:

我的平台:

PHP 和 mySQL

我的情况:

我正在尝试在我的代码中实现事务。我试图按照示例进行操作,但这并没有太大帮助。我正在运行 3 个查询,并且我想以这样的方式编写一个事务,以便如果任何查询失败,整个事务都应该回滚。我非常感谢一个简单、高效且非面向对象的 PHP 代码来实现这一目标。提前谢谢你。

我的 PHP 代码:

//db_res calls a custom function that performs a mysql_query on the query
$res1 = db_res("SELECT c1, c2 FROM t1 WHERE c5 = 3");
$res2 = db_res("UPDATE t2 SET c1 = 5 WHERE c2 = 10");
$res3 = db_res("DELETE FROM t3 WHERE c1 = 20");

if( $res1 && $res2 && $res3 )
{
 //commit --- but how?
}
else
{
 //rollback --- but how?
}

【问题讨论】:

标签: php mysql transactions commit rollback


【解决方案1】:

你不需要使用 mysqli。您可以将事务命令作为查询发出。

所以对于你的例子:

mysql_query("start transaction;");

//db_res calls a custom function that performs a mysql_query on the query
$res1 = db_res("SELECT c1, c2 FROM t1 WHERE c5 = 3");
$res2 = db_res("UPDATE t2 SET c1 = 5 WHERE c2 = 10");
$res3 = db_res("DELETE FROM t3 WHERE c1 = 20");

if( $res1 && $res2 && $res3 )
{
  mysql_query("commit;");
}
else
{
  mysql_query("rollback;");
}

如果您正在考虑升级到 mysqli,请不要这样做。而是升级到 PDO,它更加理智。

【讨论】:

  • 我认为你的意思是 mysql_query("start transaction");或 mysql_query("begin");
  • 是的,两者混淆了……会解决的。
  • mysql不安全,试试mysqli或者PDO
  • @Asuquo12 是的,绝对使用 PDO。我什至在回答的最后这么说。但是如果你在一些遗留的内部事物上使用 mysql,你可以像我上面显示的那样进行事务,而无需升级。
【解决方案2】:

您需要使用mysqli extension 才能使用此功能。

请参阅:autocommit()commit()rollback()

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

/* disable autocommit */
mysqli_autocommit($link, FALSE);

mysqli_query($link, "CREATE TABLE myCity LIKE City");
mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");

/* commit insert */
mysqli_commit($link);

/* delete all rows */
mysqli_query($link, "DELETE FROM myCity");

if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
    $row = mysqli_fetch_row($result);
    printf("%d rows in table myCity.\n", $row[0]);
    /* Free result */
    mysqli_free_result($result);
}

/* Rollback */
mysqli_rollback($link);

if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
    $row = mysqli_fetch_row($result);
    printf("%d rows in table myCity (after rollback).\n", $row[0]);
    /* Free result */
    mysqli_free_result($result);
}

/* Drop table myCity */
mysqli_query($link, "DROP TABLE myCity");

mysqli_close($link);
?>

【讨论】:

  • @John 感谢您的回复。我已经使用 mysql_query 构建了超过 85% 的应用程序。如果我必须使用 mysqli,我想我需要修改我的应用程序并从头开始重新设计它。是否有可能以任何方式使用 mysql_query 来做到这一点?我不太确定此时是否可以切换到 mysqli,因此非常感谢替代解决方案。期待您的回复。
  • 很遗憾没有。 mysql_* 函数不提供对 MySQL 4 及更高版本提供的高级功能的访问,例如事务和存储过程。您无需返回并更改对 MySQL 的每次调用即可使用 mysqli。只需更改实际需要使用它的页面即可。
猜你喜欢
  • 2012-12-24
  • 2013-11-22
  • 2010-11-24
  • 1970-01-01
  • 1970-01-01
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-01
相关资源
最近更新 更多