Jump to content

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


s0c0

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.