事务和提交

发布于 2024-11-17 09:27:24 字数 774 浏览 1 评论 0原文

哪个代码适合交易? 我需要检查提交的查询结果吗?

此代码没有任何结果

mysql_query("BEGIN");
$strSQL = "INSERT INTO table values";
$strSQL .="('','a')";
$objQuery1 = mysql_query($strSQL);
$strSQL = "INSERT INTO table values";
$strSQL .="('','a','a')";
$objQuery2 = mysql_query($strSQL);
if(($objQuery1) and ($objQuery2))
{
mysql_query("COMMIT");
echo "Save Done.";
}
else
{
mysql_query("ROLLBACK");
}
?>

,或者

此代码结果 1 插入。为什么?不会提交识别错误?

  <?php
    mysql_query("BEGIN");
    $strSQL = "INSERT INTO table values";
    $strSQL .="('','a')";
    $objQuery1 = mysql_query($strSQL);
    $strSQL = "INSERT INTO table values";
    $strSQL .="('','a','a')";
    $objQuery2 = mysql_query($strSQL);
    mysql_query("COMMIT");
    ?>

Which code is right for transaction?
do i need to check query result for commit?

this code result nothing

mysql_query("BEGIN");
$strSQL = "INSERT INTO table values";
$strSQL .="('','a')";
$objQuery1 = mysql_query($strSQL);
$strSQL = "INSERT INTO table values";
$strSQL .="('','a','a')";
$objQuery2 = mysql_query($strSQL);
if(($objQuery1) and ($objQuery2))
{
mysql_query("COMMIT");
echo "Save Done.";
}
else
{
mysql_query("ROLLBACK");
}
?>

or

this code rusult 1 insert. why?wont commit recognize the error?

  <?php
    mysql_query("BEGIN");
    $strSQL = "INSERT INTO table values";
    $strSQL .="('','a')";
    $objQuery1 = mysql_query($strSQL);
    $strSQL = "INSERT INTO table values";
    $strSQL .="('','a','a')";
    $objQuery2 = mysql_query($strSQL);
    mysql_query("COMMIT");
    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2024-11-24 09:27:24

您可能会感到困惑的是,在 MySQL 中发出commit 并不会转化为rollback [一切]错误。相反,它会翻译为:提交没有错误的内容。

mysql> create table test (id int unique);
Query OK, 0 rows affected (0.10 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'id'
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

相比之下,在 Postgres 中:

test=# create table test (id int unique);
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "test_id_key" for table "test"
CREATE TABLE
test=# begin;
BEGIN
test=# insert into test values (1);
INSERT 0 1
test=# insert into test values (1);
ERROR:  duplicate key value violates unique constraint "test_id_key"
DETAIL:  Key (id)=(1) already exists.
test=# commit;
ROLLBACK
test=# select * from test;
 id 
----
(0 rows)

另外请考虑使用 mysqli。它直接支持这种东西:

http://www.php.net/manual /en/mysqli.commit.php

或 PDO:

http://php.net/manual/en/pdo.commit.php

使用 PDO,正确的顺序将如下所示:

try {
  # begin transaction
  # do stuff
  # commit
} catch (Exception $e) {
  # rollback
}

使用 MySQLi,您也可以使其行为与上面类似使用 or 运算符:

try {
  # begin transaction
  # do stuff or throw new Exception;
  # commit
} catch (Exception $e) {
  # rollback
}

What might be confusing you is that issuing a commit in MySQL does not translate to rollback [everything] on error. It instead translates to: commit stuff that had no errors.

mysql> create table test (id int unique);
Query OK, 0 rows affected (0.10 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values (1);
Query OK, 1 row affected (0.00 sec)

mysql> insert into test values (1);
ERROR 1062 (23000): Duplicate entry '1' for key 'id'
mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from test;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

In Postgres, by contrast:

test=# create table test (id int unique);
NOTICE:  CREATE TABLE / UNIQUE will create implicit index "test_id_key" for table "test"
CREATE TABLE
test=# begin;
BEGIN
test=# insert into test values (1);
INSERT 0 1
test=# insert into test values (1);
ERROR:  duplicate key value violates unique constraint "test_id_key"
DETAIL:  Key (id)=(1) already exists.
test=# commit;
ROLLBACK
test=# select * from test;
 id 
----
(0 rows)

On a separate note, consider using mysqli instead. It supports this kind of stuff directly:

http://www.php.net/manual/en/mysqli.commit.php

Or PDO:

http://php.net/manual/en/pdo.commit.php

Using PDO, the correct sequence will look like this:

try {
  # begin transaction
  # do stuff
  # commit
} catch (Exception $e) {
  # rollback
}

Using MySQLi, you can make it behave like the above too with the or operator:

try {
  # begin transaction
  # do stuff or throw new Exception;
  # commit
} catch (Exception $e) {
  # rollback
}
梦幻的心爱 2024-11-24 09:27:24

我认为调用mysql_query("COMMIT")需要根据之前查询是否成功来确定。因此,在上面的代码中,没有任何内容提交到数据库,因为前面的 2 个查询之一失败了。

I think the calling mysql_query("COMMIT") needs to be determined by the success of previous queries. Thus, in the above code nothing is commited to db because one of the 2 previous queries fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文