Hello,

i run a browser game and sometimes players manage to interrupt a script or there might be an error somewhere... the problem is that some part of the script had already done some changes to the database for example... wich can be very bad if the script does not run untill the end...

For example you want to buy a sword, the script checks for cash, if there is cash it deducts the price of the sword but then there was an error or something and the player did not get that sword.

I want to roll back or not submit those changes unless the script runs to the end. what is the best way to achieve this? i would guess stored procedures but they can only be used with mysql code not php inbetween?

what should i do?

    You might want to look into database transactions. These allow you to run several queries as a batch, and if any one fails give you the chance to roll back. Later versions of MySQL support transactions.

      you can stop the user abort affecting the script execution with:

      ignore_user_abort(true);
      
        dagon;10931274 wrote:

        you can stop the user abort affecting the script execution with:

        ignore_user_abort(true);
        

        so if a user clicks very fast on a link that runs a big script, the script always runs to the end for sure, the web server makes sure if it?

          Shrike;10931268 wrote:

          You might want to look into database transactions. These allow you to run several queries as a batch, and if any one fails give you the chance to roll back. Later versions of MySQL support transactions.

          can i use PHP whitin a transaction ?

            peterdays;10931286 wrote:

            so if a user clicks very fast on a link that runs a big script, the script always runs to the end for sure, the web server makes sure if it?

            does what it says on the tin:
            ignore_user_abort

              peterdays wrote:

              can i use PHP whitin a transaction ?

              If you mean; can you start a transaction on the current database connection, send queries, receive results and do processing, and then commit or rollback the transaction; then yes - that's the point of transactions.

                nice to know about ignore_user_abort()... any reason why i should not be using it? seems to be only has upsides when updating database based on user interaction.

                oh i remember why i didnt start implementing transactions, tables need to be innodb, wich i am not to sure i am want to change from myisam

                  Write a Reply...