If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Insert data into DB, return ID number

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-09-10, 11:42
tlshaheen tlshaheen is offline
Registered User
 
Join Date: Dec 2009
Posts: 20
Insert data into DB, return ID number

I'm inserting an Article into a DB. The fields I'm inserting are Title, Intro, Content, Creation_Date. There is a field in the DB that is the primary key, Article_ID, which is auto-increment.

I insert the article into the DB, and it is assigned the article_id by the DB. I want to use that ID later in my code on the same page. Is there any way to get SQL to return the ID on my Insert statement, or must I do a SELECT ... WHERE statement after I do my insert?
Reply With Quote
  #2 (permalink)  
Old 03-09-10, 12:23
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Quote:
Originally Posted by tlshaheen
Is there any way to get SQL to return the ID on my Insert statement, or must I do a SELECT ... WHERE statement after I do my insert?
There's a MySQL function called LAST_INSERT_ID()
There is also a PHP function (if that's any use) called mysql_insert_id()
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 03-09-10, 12:35
tlshaheen tlshaheen is offline
Registered User
 
Join Date: Dec 2009
Posts: 20
The PHP function is perfect! Thank you very much for your response!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On