Re: Help: Transactions not working
| From: | John Holmes | Date: | Fri, 15 Oct 2004 11:27:19 +0000 |
| Subject: | Re: Help: Transactions not working | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-36822@lists.php.net to get a copy of this message | ||
Stuart Felenstein wrote:
But - I want to confirm, am I still using transactions even though I'm issuing individual query calls for each insert.Yes, that's the idea. Everthing between BEGIN and COMMIT/ROLLBACK is the transaction.
In this line (from the second insert) VALUES (null, LAST_INSERT_ID(), ......)"; The LAST_INSERT_ID works great here, getting the auto-inc from the first insert, but will it work in subsequent insertions to other tables. I ask because in the second insert (shown), the LAST_INSERT_ID, gets the value from the first table, but the second table (second insert) also has an auto-inc column. So I'm thinking then the third insert will get the LAST_INSERT_ID from table 2 / 2nd insert.Yes, that's what will happen. LAST_INSERT_ID() picks up the last auto_increment number created.
Perhaps I need to declare it the first time as a value (app code) or add "where" conditions to all the subsequent insertions ?You can get the auto_increment number using mysql_insert_id(), save it in a variable and then use that in subsequent queries. $query1 = "INSERT INTO ... "; $id = mysql_insert_id(); $query2 = "INSERT INTO (id,...) VALUES ($id,...)"; $query3 = "INSERT INTO (id,...) VALUES ($id,...)"; etc... -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com