Get SQL ID of last SQL query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Get SQL ID of last SQL query?

Post 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. ^_^.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Get SQL ID of last SQL query?

Post by John Cartwright »

mysql_insert_id() will give you the id assigned of an insert
pyromaster114
Forum Newbie
Posts: 19
Joined: Fri Mar 14, 2008 8:12 pm

Re: Get SQL ID of last SQL query?

Post by pyromaster114 »

So I can like have the mysql_insertid(mysql_query("INSERT INTO posts....etc"));
?
Thank you ^_^ I shall try it ^_^.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Get SQL ID of last SQL query?

Post by John Cartwright »

read the documentation on mysql_insert_id() :wink:
Post Reply