Page 1 of 1

Get SQL ID of last SQL query?

Posted: Mon Mar 24, 2008 7:24 pm
by pyromaster114
Okay, so basically I have a problem with my image board system i'm trying to write... I need some way of naming the image and LINKING the image to the post.
I was thinking that I could submit the post to the SQL database (the topic/text stuff...) and then get the ID number (from auto-increment ID column in SQL table) and name the image that. That way the post and image are linked by ID number.

My question is, how do I get the number of the last query submitted? I realize I could look at the last thing in the database, but if someone ELSE were to submit a post from another browser at that fraction of a second in-between the submit post text and retrieve ID number of last entry queries... it would get the wrong number.

Is there something like "return ID" or something simple I should know about SQL queries that I could use in this circumstance?

Help is much appreciated. ^_^.

Re: Get SQL ID of last SQL query?

Posted: Mon Mar 24, 2008 7:34 pm
by John Cartwright
mysql_insert_id() will give you the id assigned of an insert

Re: Get SQL ID of last SQL query?

Posted: Mon Mar 24, 2008 8:07 pm
by pyromaster114
So I can like have the mysql_insertid(mysql_query("INSERT INTO posts....etc"));
?
Thank you ^_^ I shall try it ^_^.

Re: Get SQL ID of last SQL query?

Posted: Mon Mar 24, 2008 8:16 pm
by John Cartwright
read the documentation on mysql_insert_id() :wink: