Tantalus Posted November 19, 2008 Share Posted November 19, 2008 Hello, I have a typical table called userdata, it has an auto-incrementing unsigned integer primary key named userdataid, and a bunch of other columns storing various data (firstname, lastname, gender, location, occupation, website, email, etc...). When I create a new user, I have to create their associated data, and insert those values into a new row in the userdata table. Since the primary key auto-increments, (basically setting itself on an INSERT statement, right?) my question is, how do I find out what the userdataid primary key was set to after creating a new row? for example: INSERT INTO userdata(firstname, lastname, location) VALUES('default', 'default', 'default'); If I run this statement for a new user, a new row gets created and inserted into the userdata table. This new row has a userdataid value that I need to get at. One way to tackle this would be to run this statement: SELECT userdataid, firstname, lastname, occupation FROM userdata WHERE firstname='default' and lastname='default' and occupation='default'; The problem with this is that if the user never updates their userdata from its default values, then this last statement will return a bunch of rows, not just the one that was last INSERTed, and I won't know which userdataid is the one I want to get at. So, anyone know if I'm overlooking a simple solution here, or if I'm going about this wrong? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/133298-solved-getting-an-auto-incremented-primary-key-from-a-newly-inserted-row/ Share on other sites More sharing options...
eyaleyal Posted November 19, 2008 Share Posted November 19, 2008 $id = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/133298-solved-getting-an-auto-incremented-primary-key-from-a-newly-inserted-row/#findComment-693267 Share on other sites More sharing options...
Tantalus Posted November 19, 2008 Author Share Posted November 19, 2008 Thanks so much! I figured there would have to be a straightforward way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/133298-solved-getting-an-auto-incremented-primary-key-from-a-newly-inserted-row/#findComment-693284 Share on other sites More sharing options...
revraz Posted November 19, 2008 Share Posted November 19, 2008 As easy as marking a topic Solved when solved Quote Link to comment https://forums.phpfreaks.com/topic/133298-solved-getting-an-auto-incremented-primary-key-from-a-newly-inserted-row/#findComment-693539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.