Posted at

Mysql+PHP トランザクションとテーブルロック(古い)

More than 1 year has passed since last update.

テーブルロックしなければいけない場面って、そんなに発生しないのでいつもいつも忘れてしまうのでまとめておく。

古いmysql

    mysql_query("SET AUTOCOMMIT = 0");

mysql_query('LOCK TABLES schools WRITE, students WRITE');

$query = "SELECT max(number) as number FROM students";
$query = "INSERT INTO students ....";

mysql_query("COMMIT");
mysql_query("UNLOCK TABLES");

mysqli

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$mysqli->query("SET AUTOCOMMIT = 0");
$mysqli->query('LOCK TABLES schools WRITE, students WRITE');

$query = "SELECT max(number) as number FROM students";
$query = "INSERT INTO students ....";

$mysqli->query("COMMIT");
$mysqli->query("UNLOCK TABLES");