Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

MySQL PHP Applicaton select and lock record for update

Avatar of Matthew_Way
Matthew_Way asked on
8 Comments1 Solution3464 ViewsLast Modified:
I'm developing an application which will have 100 operators in a call center updating contact records.
The application is written in PHP 5.2 / MySQL 5.1

I want some advice on the best way to resolve the problem of locking.
( I presume I can't use the MySQL locking method as time from select to update may be several minutes if ever... )

We need to progressively phone all the contacts that meet a given target criteria.

In the application the operator clicks a button "Phone Next" which will retrieve the next contact in the database.
Then they will update the record , call status etc then press "Phone Next" which will save the current record and retrieve the next contact.

When a record is retrieved for updating I will mark that record with the session ID and current timestamp.
And use this for a method of 'locking the record for update'

( If the record isn't saved after 1hr the lock will be released )

My question how do I stop a collision on this locking mechanisim.

My first thought is to update the table first, then select the record second.
UPDATE contacts
SET lockSessionId = {$sessionId}, lockTimeStamp=NOW()
WHERE lockSessionId = 0 AND callList IN ( 1,2,4 )
FROM contacts
WHERE lockSessionId = {$sessionId}

As if I where to SELECT first then apply the lock there is a risk that the same record will be allocated twice.

Any suggestions on how to handle the above programming problem.

Avatar of Beverley Portlock
Beverley PortlockFlag of United Kingdom of Great Britain and Northern Ireland image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answers