The function has next structure:
$q = 'LOCK TABLES table1 WRITE;';
mysql_query($q);
$q = 'select id from table1 where is_delete = 0 limit 1;';
$res = mysql_fetch_assoc(mysql_query($q));
if($res) {
$q = 'UPDATE table1 SET is_delete = 1 WHERE id = '".$res['id']."'';
mysql_query($q);
}
$q = 'UNLOCK TABLES;';
mysql_query($q);
I locking all tables, but queries run parallel.
How fix this?
Check whether you’re getting any MySQL errors on the LOCK TABLES query:
However, if this is all you are doing, you can also simply write:
which does not require any locks at all. Of course, this will only work if the data from your first query is not being processed in any way, and if it doesn’t really matter which row you are updating, as long as it’s one in which
is_deleteis set to 0. This is what the code you posted does, too, but it’s not immediately obvious to me what you would want to use this code for 🙂More generally, if you are using the default InnoDB storage engine for your MySQL table, you may want to look into SELECT … FOR UPDATE:
http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html
You could write:
See also: http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/