LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2008, 01:09 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
mysql: is there a way to get any information back from an insert?


I'm inserting something into a mysql database & need to know the auto-increment id of the newly inserted item after the insert. Is there a way of quickly getting that id back other than searching the database for one of the fields I just inserted? I suppose I could speed up the search query by ordering on the id & doing a limit 1, but I'm not incredibly thrilled with that idea.

So, is there some fancy way of getting information back from an INSERT?
 
Old 05-07-2008, 01:16 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
You can try "last_insert_id()":
http://dev.mysql.com/doc/refman/5.0/...tionality.html
 
Old 05-07-2008, 01:32 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by BrianK View Post
I'm inserting something into a mysql database & need to know the auto-increment id of the newly inserted item after the insert. Is there a way of quickly getting that id back other than searching the database for one of the fields I just inserted? I suppose I could speed up the search query by ordering on the id & doing a limit 1, but I'm not incredibly thrilled with that idea.

So, is there some fancy way of getting information back from an INSERT?
Plus, you could run into concurrency issues. Use Paulsm's advice.
 
Old 05-07-2008, 05:15 PM   #4
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Thanks guys. Looks like a workable solution. It still seems like there's the potential for a race fouling up the results, but it's really, really unlikely in this case.
 
Old 05-07-2008, 05:49 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you wrap SQL Transaction statements around that (and you should), then it'll only see what the current txn has done (until you do a commit/rollback).
http://dev.mysql.com/doc/refman/5.1/en/commit.html
Note that the ISAM engine does not support SQL txns, use InnoDB or NDB.
 
Old 05-09-2008, 06:51 AM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Also, last_insert_id works on a per-client basis, so as long as you aren't doing anything else with the client (ie. you control your app), then it'll work as expected. So, if someone else does an insert after you do yours, but before you do a last_insert_id, then it'll still give you the insert id of YOUR last insert, not the one that followed from someone else
 
Old 05-09-2008, 07:04 PM   #7
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by 95se View Post
Also, last_insert_id works on a per-client basis, so as long as you aren't doing anything else with the client (ie. you control your app), then it'll work as expected. So, if someone else does an insert after you do yours, but before you do a last_insert_id, then it'll still give you the insert id of YOUR last insert, not the one that followed from someone else
Oh nice. Then that's perfect.

Thanks again.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql insert on duplicate return ? Balakrishnan84 Linux - Software 2 01-04-2008 01:43 AM
mysql insert using bash script venki Linux - General 3 07-07-2007 04:52 AM
Mysql insert problem don_dimo Linux - Software 3 11-01-2003 09:27 PM
Why MySQL Error on INSERT INTO....? pnh73 Programming 2 11-01-2003 05:53 PM
perl script to insert image information rhuser Programming 1 03-12-2003 05:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration