+ Reply to Thread
Results 1 to 3 of 3

Thread: Using transactions with PHP/MySQL?

  1. #1
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Using transactions with PHP/MySQL?

    Hi!
    Has anyone managed to use transactions with MySQL and Php?
    Since the x10 server Starka seems to be having massive problems with MySQL connections there's a high probability that connections are lost between queries that need to be executed together. (It has happened that new players have signed up, but not received any activation e-mail because the database connection was lost in between actions).

    I've tried to use SET AUTOCOMMIT=0 and then use BEGIN, END and COMMIT statements.
    If I put an "exit;" (in the php code) and halts it before the COMMIT, all changes should be rolled-back right? It seems like it doesn't.
    Or is the mysqli class a better alternative?

  2. #2
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

    Re: Using transactions with PHP/MySQL?

    1. Make sure you are using a table type that supports transactions, InnoDB is the probably the best choice.
    2. This code should work:
    PHP Code:
    //$connect = PDO connection
    //Start transaction
    $connect->exec('START TRANSACTION;');

    //Do queries
    $query $connect->prepare('INSERT ETC');
    $sth->execute();

    //Or

    $connect->exec('INSERT ETC;');

    //Commit
    $connect->exec('COMMIT;'); 
    Setting autocommit shouldn't need to be done if you start a transaction, as by definition it will not commit within the transaction.

  3. #3
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Re: Using transactions with PHP/MySQL?

    Thanks! I'll try that.
    Is it correct to assume that an "exit;" in the middle of the php code would roll-back the transaction?
    Or is there another better way to simulate a lost connection? (thus rolling back the transaction.)

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 10-15-2010, 01:46 PM
  2. Replies: 2
    Last Post: 10-14-2010, 10:49 AM
  3. No Transactions to Display
    By tittat in forum Free Hosting
    Replies: 1
    Last Post: 04-27-2008, 11:24 AM
  4. Replies: 2
    Last Post: 12-17-2007, 03:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers