Home » Php » php – Lock table for reading and writing

php – Lock table for reading and writing

Posted by: admin July 12, 2020 Leave a comment


How to lock table for reading and writing with php? Here is what I tried without luck.

mysql_query("LOCK TABLES table WRITE;");
mysql_query("LOCK TABLES table READ, WRITE;");
mysql_query("LOCK TABLES table READ WRITE;");

Here is the error I got:

You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near ‘WRITE’ at line 1

How to&Answers:
mysql_query("LOCK TABLE table WRITE"); // you might think it's here
mysql_query("LOCK TABLE table READ, table AS t2 WRITE"); // <- but the error is here
mysql_query("LOCK TABLES table READ, table as t2 WRITE"); // <- ...and here.

You can not acquire multiple locks for the same table without aliasing it. Read the manual.