PHP MySQL Transaction Tutorial




We use use PHP to create a MySQL Transaction. Ill explain what a MySQL transaction is and how to use it with the MySQLi driver. Once you do understand this, …

Original source


28 responses to “PHP MySQL Transaction Tutorial”

  1. Suppose 2 concurrent transactions were trying to perform the SAME operations, how would the database handle that? For instance in a banking scenario i.e. money transfer. Wouldnt consistency get screwed up?

  2. Once I implemented this and my current boss was angry; he said mySQL transactions weren't fully suported (in 2010/July, don't know what version was using, but I'm sure was 5+)

    What do you think ? is this reliable ?

  3. There is another way you could do transactions. One thing you can do is make savepoint commands so you can roll back to a certain point instead of to the last commit so you save your progress when its at a safe point. You can build it into a mysqli helper class like this.

    function transaction_savepoint($name)
    {
    $this->query("SAVEPOINT $name");
    }

    function transaction_rollback($name = null)
    {
    $this->query("ROLLBACK" . ($name != null ? " TO SAVEPOINT $name" : ""));
    }

  4. @sachinprasad1234 It's built into PHP. If you compiled PHP by source, you will need to tell the config script that you want to enable MySQLi at compile time. But yeah, it is compiled directly into PHP.

    I'm not sure what version of PHP this affects, but in some version MySQL or even MySQLi are not shipped as standard anymore. Meaning, you have to install them yourself. This might just be for us Linux guys. Not entirely sure.

  5. @frosty1433 Because if you run a query again it might be internally stored. So you could have a bunch of rows pending in the queue- and if you run again and it succeeds, you'll get double the stuff.

Leave a Reply