|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
mySQL Transaction Rollback
I'm new to PHP & mySQL, and currently I'm working on a function which does the following: connect to db, insert into 3 different tables, close the connection. Here's the code:
$db = mysql_connect("localhost", "root", "pwd") or die("Could not connect: ".mysql_error()); mysql_select_db("HR", $db); $query = "INSERT INTO tblA (Field1, Field2) values ('a', 'b'); $sql = mysql_query($query) or die("Query Error: ".mysql_error()); $IndexNo = mysql_insert_id(); $query2 = "INSERT INTO tblB (Index, Field) values ($IndexNo, 'b'); $sql = mysql_query($query2) or die("Query Error: ".mysql_error()); $query3 = "INSERT INTO tblC (Index, Field) values ($IndexNo, 'b'); $sql = mysql_query($query3) or die("Query Error: ".mysql_error()); mysql_close($db); Now, I don't want it to autocommit. So in case there's error while inserting tblC, the insert statement for tblA and tblB will be cancelled. I know I have to use start transaction and rollback if there's an error. But I'm not sure about the coding. Can anyone help? Thanks a lot! |
|
#2
|
||||
|
||||
|
MySQL is set to autocommit by default...
Try using SET AUTOCOMMIT=0 to disable autocommiting Also realize, you must use a recent MySQL version which supports InnoDB tables Notice that when doing that you need to *always* use commit to save changes... To change it so you only autocommit in a transaction, try this: Code:
START TRANSACTION; UPDATE table SET summmary='whatever' WHERE type=1; COMMIT; PHP *does* support transactions, use seperate query statements for each command... PHP Code:
hopefully this helps a bit? MySQL documentation: http://www.mysql.com/doc/en/COMMIT.html MySQL's growing up: Transactions [web monkey]: http://hotwired.lycos.com/webmonkey...dex3a_meta.html |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > mySQL Transaction Rollback |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|