1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mysql query "failing" after a transaction

Discussion in 'PHP' started by Laurentvw, Jul 14, 2009.

  1. #1
    I'm making a simple mysql transaction (yes, using a innodb table), which gets committed. This works as expected, my two mysql queries inside the transaction were successfully executed.
    Just beneath that, I'm running a non-transactional mysql query (similar to the one above), which doesn't give me any error. "Foo" gets printed to the screen (so the script never died). Now what's weird, is that the value 333 didn't get inserted into the database. And I don't know why, everything seems fine, I'm going nuts, heh!
    Any query beneath the commit just doesn't seem to work.

    Your help is appreciated, thanks! :)

    (On a sidenote, this code does not represent a good way of making mysql transactions in php, it's just for testing purposes)

    
    <?php
    mysql_connect("localhost", "test", "test") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    
    mysql_query("SET AUTOCOMMIT=0") or die(mysql_error());
    mysql_query("START TRANSACTION") or die(mysql_error());
    mysql_query("INSERT INTO crawl_ticket(box) VALUES('82')") or die(mysql_error());
    mysql_query("UPDATE crawl_ticket SET box = '72' WHERE box = '82'") or die(mysql_error());
    mysql_query("COMMIT") or die(mysql_error());
    
    mysql_query("INSERT INTO crawl_ticket(box) VALUES('333')") or die(mysql_error());
    echo 'foo';
    ?>
    
    PHP:

     
    Laurentvw, Jul 14, 2009 IP
  2. anthonywebs

    anthonywebs Banned

    Messages:
    657
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try it without the transaction and commit and see what happens
     
    anthonywebs, Jul 14, 2009 IP
  3. Laurentvw

    Laurentvw Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Of course it will work without the transaction, then it's just a simple mysql query call.
     
    Laurentvw, Jul 15, 2009 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    mysql_query("SET AUTOCOMMIT=0") or die(mysql_error());

    You never set AUTOCOMMIT back, so the last insert " crawl_ticket(box) VALUES('333') " would never be inserted. If you started a new connection you would be fine however, since your connection was never destroyed, AUTOCOMMIT would not revert.

    Also, you may want to switch to mysql improved extension. It handles transactions in much better.
     
    jestep, Jul 15, 2009 IP
    Laurentvw likes this.
  5. Laurentvw

    Laurentvw Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Good to know. Thanks very much for the helpful reply! I appreciate it :)
     
    Laurentvw, Jul 15, 2009 IP