How does php execute mysql row lock?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.