[Solved]Retrieving auto_increment data after row is written.

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
ctown82
Forum Newbie
Posts: 11
Joined: Mon Oct 01, 2007 9:29 am

[Solved]Retrieving auto_increment data after row is written.

Post 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.
Last edited by ctown82 on Tue Oct 23, 2007 4:53 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

ctown82
Forum Newbie
Posts: 11
Joined: Mon Oct 01, 2007 9:29 am

Post by ctown82 »

I love you. :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Feeling the love. 8)
Post Reply