2010-10-25 15:59
浏览 332


If I run the following PHP, I would expect no value to be inserted into the test table, because I have a transaction that I haven't committed:

$db = mysql_connect("localhost","test","test");
mysql_query("begin transaction;");
mysql_query("insert into Test values (1);") or die("insert error: ". mysql_errror());
die('Data should not be commited
mysql_query("commit;"); // never occurs because of the die()

But instead it seems to commit anyway. Is there a way to turn off this behaviour without turning off autocommit for the PHP that doesn't use transactions elsewhere on the site?

Edit: This was just a stupid typo. It should be "start transaction" or "begin". Not "begin transaction". Sorry to waste peoples time.

图片转代码服务由CSDN问答提供 功能建议

如果我运行以下PHP,我希望没有值插入测试表,因为我有一个 我尚未提交的事务:

  $ db = mysql_connect(“localhost”,“test”,“test”); 
mysql_query  (“begin transaction;”); 
mysql_query(“insert into Test values(1);”)或die(“insert error:”。mysql_errror()); 
die('Data not not commited 
mysql_query( “提交;”);  //永远不会因为die()

而发生但反正似乎无论如何都要提交。 有没有办法关闭此行为而不关闭不使用网站上其他地方的事务的PHP的自动提交?

编辑:这只是一个愚蠢的错字。 它应该是“开始交易”或“开始”。 不是“开始交易”。 抱歉浪费时间。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongmeng0317 2010-10-25 16:07

    Use mysql_query('BEGIN'). The SQL "BEGIN TRANSACTION" is not valid (and in fact mysql_query is returning false on that query, which means there is an error). It's not working because you never start a transaction.

    打赏 评论
  • dougan7657 2010-10-25 16:18

    The syntax to start a transaction is:


    The feature you are talking about is AUTOCOMMIT. If you don't want it, you'll have to disable it:

    SET autocommit = 0

    The reference can be found at http://dev.mysql.com/doc/refman/5.1/en/commit.html

    I also recommend that you test the return value of all mysql_...() functions. You cannot assume that they'll always run successfully.

    打赏 评论
  • dtp0760 2012-12-24 16:12

    By default, the transaction will not be rolled back. It is the responsibility of your application code to decide how to handle this error, whether that's trying again, or rolling back.

    If you want automatic rollback, that is also explained in the manual:

    The current transaction is not rolled back. To have the entire transaction roll back, start the server with the `--innodb_rollback_on_timeout` option.
    打赏 评论

相关推荐 更多相似问题