Jump to content


Photo

get inserted id(auto) from insert statement


  • Please log in to reply
4 replies to this topic

#1 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 May 2006 - 09:57 AM

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\" /] **
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 May 2006 - 10:32 AM

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.

#3 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 11 May 2006 - 10:58 AM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 11 May 2006 - 12:41 PM

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.

Info: PHP Manual


#5 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 12 May 2006 - 08:43 PM

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() FROM table [!--sql2--][/div][!--sql3--]
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users