Jump to content

Archived

This topic is now archived and is closed to further replies.

shocker-z

get inserted id(auto) from insert statement

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\" /] **

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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.

Share this post


Link to post
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--]

Share this post


Link to post
Share on other sites

×

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.