NYCPHP Meetup

NYPHP.org

[nycphp-talk] PEAR::DB insertID???

Eric Rank erank at isthmus.com
Tue Mar 30 11:16:53 EST 2004


Thanks for the tips. mysql_insert_id() it is.

Although I am curious about the forementioned sequences. I'll look a little
deeper.

For some context:

Perhaps it's obvious, perhaps it's not, the whole point of grabbing the id
of an auto-incremented record is to records to related tables using the
inserted id as a foreign key. For example, after insterting a record into a
'user' table, I need to use the id of that user as the 'userid' in an
'emailaddresses' table. I just imagined that the best way to do this is to
use something analagous to mysql_inserted_id() to get the id. Is there a
better way to do a series of related inserts?

Eric.


----- Original Message ----- 
From: "Adam Maccabee Trachtenberg" <adam at trachtenberg.com>
To: "NYPHP Talk" <talk at lists.nyphp.org>
Sent: Tuesday, March 30, 2004 9:57 AM
Subject: Re: [nycphp-talk] PEAR::DB insertID???


> > I'm not using PEAR::DB (yet), but what I do with MySQL when I need to
> > find this out is:
> >
> > 1) Lock the table
> > 2) Insert what needs to get inserted
> > 3) Query the ID out of the table order by id desc limit 1
> > 4) Unlock the table
>
> Bleach. :) Use mysql_insert_id() instead and you can skip the table
locking.
>
> http://us4.php.net/manual/en/function.mysql-insert-id.php
>
> Yes, it's smart enough to return your insert id even if a different
> process inserts another row into the table in between your insert and
> the call to mysql_insert_id().
>
> -adam
>
> -- 
> adam at trachtenberg.com
> author of o'reilly's php cookbook
> avoid the holiday rush, buy your copy today!
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list