Re: Help: Transactions not working

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux