Page 1 of 1

[Solved]Retrieving auto_increment data after row is written.

Posted: Tue Oct 23, 2007 4:10 pm
by ctown82
Here's the deal... I'm separating contact info and user info in my database. Reason being: I'm going to be adding client (non-user) contact data in, and would like to minimize database usage.

For example, every user is going to have to have their contact information listed, so is every client. I've made a 'contact' table in addition to my 'user' table, and a column in my 'user' table points to the auto-incrementing index of the 'contact' table. That way, I don't have to have fields in both 'client' and 'user' which store the same type of data.

My problem is... on creating a new user, how would I add him as a contact, and retrieve 'contact.contact_id' as I do the insert? I could probably just do a search, and use the derived ID from the (hopefully) only contact with the same data I just wrote, but that seems a little... inefficient.

Would this work?

Code: Select all

$result = mysql_query("INSERT INTO contact (firstname, lastname, address, phone) VALUES ('Joe', 'Schmoe', '123 Street RD', '8885551212')");
$row = mysql_fetch_assoc($result);
echo $row['contact_id'];
I don't have it in front of me, which is why I'm asking... just something I was pondering while I was at work.

Posted: Tue Oct 23, 2007 4:18 pm
by Kieran Huggins

Posted: Tue Oct 23, 2007 4:23 pm
by ctown82
I love you. :)

Posted: Tue Oct 23, 2007 8:34 pm
by Kieran Huggins
Feeling the love. 8)