Re: MySQL INSERT
| From: | David Robley | Date: | Mon, 19 Nov 2001 05:08:02 +0000 |
| Subject: | Re: MySQL INSERT | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-75069@lists.php.net to get a copy of this message | ||
On Mon, 19 Nov 2001 15:19, Justin French wrote:
> Thanks David,
>
> I've skipped off to the manual and done a bit of reading, but i'm a
> little confused. The user-added notes have actually clouded the issue
> more!!
>
> It *looks like* from what they're saying, that the number returned may
> not be the number that I want, if someone else inserted just after me.
> They go to talk about locking and all sorts of junk, but I was hoping
> you could clarify the bit about an idenitifier.
>
> How do I specify an identifier upon INSERT, and how do I re-use it when
> using mysql_insert_id?
>
> Or is all of this implied by PHP/MySQL?
>
>
> Many thanks,
>
> Justin French
>
> > mysql_insert_id is your friend, in this case. Even if someone inserts
> > another record, this function will return the auto ID for the last
> > insert performed using a given link identifier. So you feed it the
> > link identifier for _your_ insert, which of course you know, and
> > Bob's yer uncle :-)
The link identifier is the value returned by your call to mysql_connect,
if you do one. Otherwise, mysql_insert_id will use the last opened link,
which will be the current link you just used to insert the data. Assuming
this is all done in the same script, of course. If you do an insert in
this script, then jump to another script and try to get the last inserted
ID, you're in trouble.
But essentially, if you do
mysql_db_query($DB, 'INSERT INTO blah');
$newid = mysql_insert_id();
you can be assured that the $newid will be the ID of the record inserted
by the previous line, notwithstanding that forty other people are
inserting new records almost simultaneously.
--
David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc
CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA
Oxymoron: Split level.