Jump to content

get inserted id(auto) from insert statement


shocker-z

Recommended Posts

Hi there fella's and lasses..

im running this code
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
$count_add_q=mysql_query("INSERT INTO countdowns (username, date, subject, description, txtcolor, bgcolor) VALUES('$session_username','$count_date','$count_subject','$count_description','$count_txtcolor','$count_bgcolor')");
[/quote]

Just wondered if there is a way that from that insert statement i can also grab the ID that is added auto.. Recently i have basicaly done a select statement to select all the exact data that has been inserted but i feel that there must be an easier way??

Regards
Liam (stuck at work while sun is out and blue skys (peeping thru the small window at end of tech callcentre :( ))


**yes that was a vote for sympathy [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] **
Link to comment
Share on other sites

I believed you are look for [a href=\"http://uk.php.net/manual/en/function.mysql-insert-id.php\" target=\"_blank\"]mysql_insert_id[/a]

This funtions returns the the previous auto_incremented row. Click the link above for more information on the function.
Link to comment
Share on other sites

That will work in most cases, but if you have a lot of concurrent inserts, it's going to be wrong part of the time. mysql_insert_id() always uses the same resource to get the correct ID value.
Link to comment
Share on other sites

agree with ober. MAX(ID) might not always be the one you've just inserted for that particular connection. if you prefer sql, try
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] LAST_[span style=\'color:blue;font-weight:bold\']INSERT[/span]_ID() [color=green]FROM[/color] [color=orange]table[/color] [!--sql2--][/div][!--sql3--]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.