www.webdeveloper.com
Results 1 to 5 of 5

Thread: COMMIT and ROLLBACK with php

  1. #1
    Join Date
    Jun 2013
    Posts
    20

    COMMIT and ROLLBACK with php

    hi!
    i am trying to make a script that updates some tables in my database and what i want to do is to commit the changes if the statements success or rollback if any of the statements fail.
    i search on the internet how to do it but all i was able to found is the mysqli method. but in my scripts i don't use the mysqli methot because a lot of people says that it is old and obsolete, so i use the PDO wich is better (or not?)
    is it a way to use COMMIT and ROLLBACK with the PDO object? thx
    here is my code.




    try
    {
    $connection = new PDO('mysql:host=localhost;dbname=uvm', 'abcd', 'xxxx');
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $connection->exec('SET NAMES "utf8"');
    }

    catch (PDOException $e)
    {
    echo "Error";
    exit();
    }



    try
    {
    $sql = "UPDATE users SET Last_Login = CURDATE() WHERE Id = :Id";
    $s = $connection->prepare($sql);
    $s->bindValue(':Id', $_SESSION['Currentuserid']);
    $s->execute();
    }
    catch (PDOException $e)
    {
    echo "Error";
    exit();
    }


    try
    {
    $sql = "UPDATE account SET Credits = 5000 WHERE Id = :Id";
    $s = $connection->prepare($sql);
    $s->bindValue(':Id', $_SESSION['Currentuserid']);
    $s->execute();
    }
    catch (PDOException $e)
    {
    echo "Error";
    exit();
    }

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    http://www.php.net/manual/en/pdo.begintransaction.php
    http://www.php.net/manual/en/pdo.commit.php
    http://www.php.net/manual/en/pdo.rollback.php

    (Of course, you need to be using a DBMS that supports transactions, and in the case of MySQL a storage engine that supports it, too, such as INNODB.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2013
    Posts
    20
    Quote Originally Posted by NogDog View Post
    http://www.php.net/manual/en/pdo.begintransaction.php
    http://www.php.net/manual/en/pdo.commit.php
    http://www.php.net/manual/en/pdo.rollback.php

    (Of course, you need to be using a DBMS that supports transactions, and in the case of MySQL a storage engine that supports it, too, such as INNODB.)

    of course of course!
    i use mysql with InnoDB engine.
    can you please give me an example of how to do commit and rollback using the PDO object???
    THX

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    Did you look at the 3 links to the applicable PDO methods? They all also link to: http://www.php.net/manual/en/pdo.transactions.php
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2013
    Posts
    20
    Quote Originally Posted by NogDog View Post
    Did you look at the 3 links to the applicable PDO methods? They all also link to: http://www.php.net/manual/en/pdo.transactions.php
    sory, when i first look at your reply i didn't see the links.
    now it's working!
    thx

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles