php对mysql的锁表操作示例
$conn = mysql_connect('127.0.0.1','db','db') or die('DB connection failed!');
mysql_select_db('tb',$conn);
mysql_query('set names "GBK"');
mysql_query('SET AUTOCOMMIT=0');设置mysql不自动提交,需自行用commit语句提交
$sql1 = "INSERT INTO `tb`(`uid`,`type`,`target_id`) VALUES(1,1,1);";
$sql2 = "INSERT INTO `tb`(`uid`,`type`,`target_id`) VALUES(2,2,2);";
//mysql_query("LOCK TABLES `tb` WRITE");//锁定表
$res1 = mysql_query($sql1);
$res2 = mysql_query($sql2);
//mysql_query("UNLOCK TABLES");//解除锁定
if($res1 && $res2){
mysql_query('COMMIT');
}else{
mysql_query('ROLLBACK');
}
mysql_query("SET AUTOCOMMIT=1");
mysql_query('END');