How do I stop a query from running if the row is locked? I have a section of code that locks the database, updates it and then confirms.

This is all working fine, if another user attempts to update the same row they cannot and their changes are discarded.

My problem is that there is a variable that is updated each time the query is run, this increases whether the query was successful or not.  So what I need is the query NOT to run in the first place if the row is locked, how do I do this?

James
you can use GET_LOCK() to avoid the query to run if other query is running on the same lock name.

you will basically do:

SELECT IS_FREE_LOCK('maintenance');
#IF RESULT IS 1
SELECT GET_LOCK('maintenance', '10');#it will get a lock named maintenance
#PERFORM ALL YOUR MAINTENANCE
SELECT RELEASE_LOCK('maintenance'); #it will release a lock named maintence


what will happen is that the thread 1 will get a lock named maintenance, while this thread don't release this lock, the command SELECT IS_FREE_LOCK('maintenance'); won't return 1.