How does php execute mysql row locking to lock a row,
Require_once ("config. inc. php"); // database connection
Mysql_query ("set autocommit = 0 ");
Mysql_query ("BEGIN ");
Mysql_query ("SELECT * FROM 'news' where id = 1 for update ");
Echo "test ";
?>
Then I execute this statement in the ie window and execute it normally.
I opened a window in chrome, opened phpmyadim, and executed the SQL statement.
Delete FROM 'news' where id = 1
The first row was deleted, indicating that the lock was not successful,
What should I do in php?
Reply to discussion (solution)
If you use for update for the storage engine, the row that is checked by the query will be locked until the current transaction ends.
Apparently, your test does not conform to the description.
If you use for update for the storage engine, the row that is checked by the query will be locked until the current transaction ends.
Apparently, your test does not conform to the description.
Thanks to the moderator for replying, the most important thing I do not know how to write in php. is the above statement correct?
I mainly want to know how to write pessimistic locking lines in php. Ask your friends for help
There is nothing wrong with what you wrote, but it is wrong to pass the test.
The lock (table lock and row lock) is only valid for the current connection. if the connection is closed, the lock is unlocked.
Your test design should be executed in program
SELECT * FROM 'news' where id = 1 for update
Program B executes
Delete FROM 'news' where id = 1
This degree is hard to grasp, and it won't work late.
There is nothing wrong with what you wrote, but it is wrong to pass the test.
The lock (table lock and row lock) is only valid for the current connection. if the connection is closed, the lock is unlocked.
Your test design should be executed in program
SELECT * FROM 'news' where id = 1 for update
Program B executes
Delete FROM 'news' where id = 1
This degree is hard to grasp, and it won't work late.
Thank you, moderator. it's okay to know what to write.