LAST_INSERT_ID() question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lapith
Forum Newbie
Posts: 11
Joined: Fri Jul 27, 2007 4:37 pm

LAST_INSERT_ID() question

Post by lapith »

I was planning to use mysql_insert_id(), and then I read the note stating that it will not work correctly if the ID datatype is BIGINT (which it is in my case). So I at least know that I have to use LAST_INSERT_ID(), however I am a little worried about using this function because I am not sure how it will act with multiple users connecting to the database at the same time. Is it possible that one user using the site will cause a database insert and then before the LAST_INSERT_ID() function call a second user could cause another insert thus throwing the accuracy of the LAST_INSERT_ID() function call? Is it possible that I have to set up some sort of transaction? If I do have to set up a transaction, how do I do so?

Thanks in advance for the help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Concurrency issues are always possible.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Use InnoDB, not MyISAM storage engine.
There are 10 types of people in this world, those who understand binary and those who don't
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

mysqli_insert_id();
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Post by thinsoldier »

VladSun wrote:Use InnoDB, not MyISAM storage engine.
I've always wondered whether it was ok to use one of the other engines. Why do [s]u[/s] you suggest InnoDB? What are it's benefits? Is it your favorite?
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Is it not much easy to keep your current database and just use the mysqli extension which does not have issues with getting the last insert id on bigint fields?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

lapith wrote:Is it possible that one user using the site will cause a database insert and then before the LAST_INSERT_ID() function call a second user could cause another insert thus throwing the accuracy of the LAST_INSERT_ID() function call?
No, LAST_INSERT_ID() works per connection.
Post Reply