Re: Re: Getting last record ID created from DB
| From: | Myron Turner | Date: | Sat, 17 Mar 2007 14:43:27 +0000 |
| Subject: | Re: Re: Getting last record ID created from DB | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-250755@lists.php.net to get a copy of this message | ||
Colin Guthrie wrote:
Philip Thompson wrote:An earlier post called attention to a concurrency problem. Wouldn't getting the last inserted ID from LAST_INSERT_ID() suffer from the same limitations as any of the other solutions which do a select to get the last ID? I assume if you are completely in control of the database, you could create a lock file using flock() and remove the lock once the Id is retrieved using any of these methods. I guess what I'm wondering is whether the simplest suggestion is the one that would use the email address as a condition in the WHERE clause to extract the ID? -- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/For auto increment values, you don't have to specify the id. For example: INSERT INTO t_users (f_name, l_name, e_mail, b_date, pic) VALUES ('$f_name', '$l_name', '$e_mail', '$b_date', null); Then to find the latest entry: SELECT user_id FROM t_users ORDER BY user_id DESC limit 1;This is not the cleanest way and some databases do not actually increment auto id fields (e.g. they could fill in the blanks from previous deletes etc.). Much better is to use the function in the MySQL API to get the insert id or if you really must use SQL, just run "SELECT LAST_INSERT_ID();" which does much the same thing. Col My own knowledge of mysql is about 5 years old and never really used. But I was recently asked to do something that required some mysql (noting too much, fortunately for me), so I've been doing some reading and am interested in questions that come up on the list.