s0c0 Posted October 17, 2007 Share Posted October 17, 2007 I have two questions. I lock writing to a table so I can grab the last inserted auto increment primary key value. The entire code looks like this: mysql_query("LOCK TABLES wsale_products WRITE"); $sql = "SELECT LAST_INSERT_ID(prod_id) as id FROM wsale_products ORDER BY id DESC LIMIT 1"; $result = mysql_query($sql); $row = mysql_fetch_array($result); mysql_query("UNLOCK TABLES"); 1: I have found no way to unlock this specific wsale_products table. I have found only a generic UNLOCK TABLES query. It seems to me that this would unlock every table in the databases rather than just the wsale_products table. Is there a way to specify which table I want to unlock? My worry is that this method is conflicting with other database transactions. 2: Notice in my select last insert id statement that I have to order by and limit to 1 to retrieve that id. Without those parameters the query returns every id in the table. Am I utilizing this mysql function incorrectly? We are running MySQL 4.x and PHP 4.x, thanks in advanced. (Yes I am aware of the PHP mysql_insert_id function). Quote Link to comment Share on other sites More sharing options...
fenway Posted October 17, 2007 Share Posted October 17, 2007 First , you DO NOT need to lock the tables -- LAST_INSERT_ID() is thread-safe. Second, all you need in SELECT LAST_INSERT_ID() ... that's it. Also, there's a php function for this purpose, too. Quote Link to comment Share on other sites More sharing options...
s0c0 Posted October 18, 2007 Author Share Posted October 18, 2007 Thanks, and yes I am aware of the php function. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.