Jump to content

[SOLVED] 2 Questions: Locking/Unlocking a table and LAST_INSERT_ID()


Recommended Posts

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).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.