I've made the switch recently from SQL Server to MySQL, and I can't find a solid enough answer to this question anywhere:
I'm using PHP, MySQL, and the InnoDB table engine on the tables I need to lock. So for a standard series of 3 statements:
- Insert into table A
- $id = $mdb2->lastInsertID()
- Insert into table B (name, fk) VALUES ('foo', $id)
what steps do I need to take to make sure $id has the value of the insert from step 1? Is it fine the way it is? Does everything need to be in a transaction? Do I need to add other queries to lock and release the tables?
Thanks guys.
Better to use transaction if any query fails at least other updates can be rolled back.
OR handle by yourself
If the step 3 fails you will receive error message from mysql store that message in variable and check for that if received delete the record inserted in 1 step.
That is all what transaction handle. You should use transaction.