Locking a MySQL database so only one person at once can run a query?

前端 未结 4 1179
孤街浪徒
孤街浪徒 2021-01-03 04:57

I am having a few issues when people are trying to access a MySQL database and they are trying to update tables with the same information.

I have a webpage written u

4条回答
  •  孤城傲影
    2021-01-03 05:19

    You're looking for LOCK.

    http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html

    This can be run as a simple mysql_query (or MySQLi::query/prepare).

    I'd say it's better to lock specific tables (although you can probably try LOCK TABLES *) that need to be locked rather than the whole database - as nothing will be able to be read. I think you're looking for something like:

    LOCK TABLES items;
    START TRANSACTION;
    INSERT INTO items (name, label) VALUES ('foo', 'bar');
    UNLOCK TABLES;
    

    Or in PHP:

    mysql_query('LOCK TABLES items');
    mysql_query("INSERT INTO items (name, label) VALUES ('foo', 'bar')");
    mysql_query('UNLOCK TABLES');
    

提交回复
热议问题