mySQL - Apply a row level lock using mysqli

佐手、 提交于 2019-12-10 10:14:17

问题


Using PHP's mysqli how do you apply a row level lock?

Row level locks stop anyone editing currently present rows that match your criteria right? but do they stop a user inserting a row which matches your criteria?

Thanks


回答1:


if you want to lock a specific row from editing, use FOR UPDATE at the end of a SELECT query. this locks the row in your transaction and prevents other users from updating it. this only works in transactional storage engines like innodb.

in answer to your questions, yes, row level locks "stop anyone editing currently present rows that match your criteria". more specifically, if (inside a transaction) you INSERT, UPDATE or DELETE a row, that row is locked from editing by anyone else until your COMMIT your transaction. if you SELECT a row using FOR UPDATE then that also locks the row.

however, this does not "stop a user inserting a row which matches your criteria".




回答2:


What is it you're trying to achieve? The InnoDB engine uses row level locking but this is an automatic internal MySQL process.



来源:https://stackoverflow.com/questions/1729457/mysql-apply-a-row-level-lock-using-mysqli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!