Re: db mysql_insert_id() question
| From: | Stig S. Bakken | Date: | Sun, 23 Jun 2002 07:56:46 +0000 |
| Subject: | Re: db mysql_insert_id() question | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-1744@lists.php.net to get a copy of this message | ||
On Thu, 2002-06-20 at 17:34, David McInnis wrote:
> I need to get the id of the last inserted record. Normally I would call
> mysql_insert_id(). But since I am moving everything to Pear it is my
> understanding that I need to use a call to nextid() to retrieve the next
> id PRIOR to insert. Is this my only option?
>
> Here is my concern. I am converting my entire app (which is live) to
> pear. But I have some applications that will still be using the
> mysql_insert_id() post insert. So, conceivably I could call nextid()
> method and get a record id of say 151 and then someone could insert
> under the non pear method and get a mysql_insert_id of 150 prior to my
> insert under pear. I would then get an error under pear when I go to do
> my insert because I would now have a dup key?
>
> Am I correct in my concern?
Yes. This is a tricky issue, but the "solution" is to _not_ have
auto-increment columns in your tables, and use _only_ the
sequence-emulation table.
If that doesn't work for you, use mysql_insert_id($db->connection), but
be aware that this may not work in future major versions of DB (1.2 ->
1.3 will not break it, but 1.2 -> 2.0 may).
- Stig
--
Stig Sæther Bakken, Fast Search & Transfer ASA, Trondheim, Norway
http://pear.php.net/wishlist.php/ssb