0

I have looked but cannot see this query anywhere. That may be because it is a stupid question - I don't know what I don't know!

I'm half way through a closed-user beta with many horrors and more security holes than a Swiss cheese. It is just a proof-of-concept thing to see if anyone is interested. It will eventually be properly coded by someone who knows what they are doing. (At the moment it is just me seeing it.)

I have hit a new problem. Everything is going nicely but sometimes if I hit a PHP error the wrong data are written to the database which causes problems later on when the data are read. It is perfectly valid data so the DB does not throw an error but I want to crash out and go and fix it WITHOUT any data staying in the DB until the PHP is fixed.

I have not used beginTransaction commit rollback yet at all (I struggle with PDO a bit) but it seems the right area for this problem.

SO....

Is there a way of putting a beginTransaction at the start of a fairly busy "processing" file and then do a commit at the end and have it fail on a PHP fail?

If there is a solution then I will go away and redo files so that I can use this otherwise, for this very untidy beta, I will ignore beginTransaction etc

Grateful for any pointers cannot find this info anywhere. I know if there is a way of doing it there will be lots of answers but if so, I must be be using bad search strings.

EDIT: PS I should have said PHP warnings and errors. Most of my problems are associated with warnings so I end up with 0 stored in the DB when it should be 150 because a variable is misnamed due to a typo - that sort of thing.

BeNice
  • 2,165
  • 2
  • 23
  • 38

1 Answers1

1

Is there a way of putting a beginTransaction at the start of a fairly busy "processing" file and then do a commit at the end and have it fail on a PHP fail?

Yes.

If you want your transaction fail on any PHP error, then just create a simple error handler like one here and put your DB stuff between begin and commit.

Thus a PHP error will become an exception, an uncaught exception is a fatal error, on a fatal error PHP aborts, on abort PHP will close a mysql connection and Mysql on close will rollback all the queries that were executed.

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I **think** I get it.(Pls remember my level of expertise is way lower than yours.) If I put the linked function written by the OP at the top of the file it will convert warnings to errors and a real error would cause the execution to stop and hence cause the rollback? – BeNice Mar 13 '16 at 15:10
  • Link in English http://php.net/manual/en/function.set-error-handler.php I have given up on this for now. May return later. The link in the answer goes to a question that seems unresolved. Sorry have not understood your answer. Will come back later when hopefully skill level will have improved. – BeNice Mar 15 '16 at 14:23