mysql way to make a lock in a php page

Posted by Cris on Stack Overflow See other posts from Stack Overflow or by Cris
Published on 2010-06-09T08:37:25Z Indexed on 2010/06/09 8:42 UTC
Read the original article Hit count: 399

Filed under:
|

Hello, i have the following mysql table:

myTable:

id int auto_increment

voucher int not null

id_user int null

I've populated voucher field with values from 1 to 100000 so i've got 100000 records; when a user clicks a button in a PHP page, i need to allocate a record for the user so i make something similar like:

update myTable set id_user=XXX where voucher=(SELECT * FROM (SELECT MIN(voucher) FROM myTable WHERE id_user is null) v);

The problem is that I don't use locks and i should use them because if two users click in the same moment i risk to assign the same voucher to different persons (2 updates in the same record so i lose 1 user) ...

I think there must be a correct way to do this, can you help me please? Thanks ! cris

© Stack Overflow or respective owner

Related posts about mysql

Related posts about lock