• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

MySQL Help

txrandom

Diamond Member
I have a database of entries with unique ids that are automatically created/incremented. So when I insert an entry in the database, I insert all the information besides the Id.

Is there anyway to get this Id when you insert information?

The only way I can think of doing it right now is:

INSERT Data1 Data2 Data3

SELECT id WHERE data1='$data1', data2='$data2'...


Is there anyway I can insert data and have the ID returned in the process?

I'm probably overlooking something very easy...
 
You can use the LAST_INSERT_ID() function. Something like this: "SELECT LAST_INSERT_ID() FROM tablename".

If you happen to be using PHP with MySQL, you can use the PHP function mysql_insert_id(). That would go like this: $id_num = mysql_insert_id($connection_link, optional).

Reference: MySQL function, PHP function
 
It is @@IDENTITY in MS SQL if anyone cares when searching in the future. 😉
 
Originally posted by: Evadman
It is @@IDENTITY in MS SQL if anyone cares when searching in the future. 😉

I'd highly recommend using SCOPE_IDENTITY() if you are getting into any kind of complex operations - especially when using triggers. @@IDENTITY returns the last identity value inserted on your connection, where as SCOPE_IDENTITY() returns the identity only within scope (that Stored Proc, Trigger, etc).

Given the difference in the two, I'm not sure @@IDENTITY should ever be used.
 
Back
Top