Thanks (to all who replied)

If I lock the table however, if another user is trying to insert (via php
page) another record they will get an error right and I will need to make a
wait+retry script?

Thanks!

-----Original Message-----
From: Tobias Asplund [mailto:[EMAIL PROTECTED] 
Sent: Monday, December 15, 2003 12:56 PM
To: Paul Fine
Cc: [EMAIL PROTECTED]
Subject: Re: Best way to get value of autoincriment after inserting NULL?

On Mon, 15 Dec 2003, Paul Fine wrote:

> If I have a table like with a column being the PK for the table and being
an
> Auto Increment value, what is the best way to return this value to my
> script?

If you insert a row LAST_INSERT_ID() will return the primary key value in
this setup.

The other way is if you're not inserting a record, but just want to know
the next value that will be used.
SHOW TABLE STATUS LIKE '<tablename>';


> It is possible that additional rows may have been added during the
> small wait.

It is possible another value is inserted in between the query for the next
number and an insert afterwards.
To stop this from happening you can place a read-lock on the table, this
won't allow any other processes to insert rows until you release the lock.

LOCK TABLE <tablename> READ;
Get Auto-increment value

Do your stuff...

UNLOCK TABLES;

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to