shocker-z Posted May 11, 2006 Share Posted May 11, 2006 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??RegardsLiam (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\" /] ** Quote Link to comment https://forums.phpfreaks.com/topic/9535-get-inserted-idauto-from-insert-statement/ Share on other sites More sharing options...
wildteen88 Posted May 11, 2006 Share Posted May 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9535-get-inserted-idauto-from-insert-statement/#findComment-35218 Share on other sites More sharing options...
shocker-z Posted May 11, 2006 Author Share Posted May 11, 2006 thanks wildteen i found another way which was stated on that page which works great too :)$result = mysql_query("SELECT MAX(ID) AS LAST_ID FROM table");just incase anyone wonderd Quote Link to comment https://forums.phpfreaks.com/topic/9535-get-inserted-idauto-from-insert-statement/#findComment-35224 Share on other sites More sharing options...
ober Posted May 11, 2006 Share Posted May 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/9535-get-inserted-idauto-from-insert-statement/#findComment-35243 Share on other sites More sharing options...
ryanlwh Posted May 12, 2006 Share Posted May 12, 2006 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--] Quote Link to comment https://forums.phpfreaks.com/topic/9535-get-inserted-idauto-from-insert-statement/#findComment-35288 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.