views:

381

answers:

2

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

A: 

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

RMcLeod
+3  A: 

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".

longneck
It should be stressed that row level locking is only available in InnoDB and not MyISAM.
Greg K
Ah, just seen the innodb tag on this :)
Greg K