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! 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(); 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. 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 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
Archived
This topic is now archived and is closed to further replies.