Mysql数据库加锁与解锁(转)

转自:http://www.pai7.cn/article.asp?id=359

SQL语句:

LOCK TABLES tablename WRITE;
LOCK TABLES tablename READ;
Insert INTO assignment VALUES (1,7513,'0000-00-00',5),(1,7513,'2003-01-20',8.5);
UNLOCK TABLES;

对于多个用户同时提交表单,并且同时向数据库中得到表单ID,我是这样解决的:

mysql_query("lock tables po read");
mysql_query("lock tables po write");

mysql_query("update po set id=id +1"));// increase po id

$sql = "Select id FROM po";
$result = mysql_query($sql);
if ($row = mysql_fetch_assoc($result)) {
    echo $row["id"]; // this order will use this id
}

mysql_free_result($result);
mysql_query("unlock tables");

posted on 2012-12-11 11:55  davidkam  阅读(798)  评论(0编辑  收藏  举报