mysql: is there a way to get any information back from an insert?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.