LoginSignup
1
2

More than 5 years have passed since last update.

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

Posted at

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

古い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");
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2