Posted by Colin McKinnon on 09/13/05 12:02
Bill Godfrey wrote:
> gordon@hammy.burditt.org (Gordon Burditt) wrote:
>> If you perform a query like:
>> update foo set bar=bar+1 where id = '$userIdx';
>
> Ah ha. I must confess, I only know SQL from cribbing the commands
> phpMyAdmin creates. I didn't realise you could do that.
>
>> it should be atomic in the database without the need for explicit
>> locks, as it's all done in one SQL statement.
>
> If the database server (MySQL) will enforce atomic access, thats perfect.
>
Presumably because you want to read either the prev or new value? In that
case you'll need to lock the tables first:
LOCK TABLES foo READ;
SELECT bar FROM foo WHERE id = '$userIdx';
UPDATE foo SET bar=bar+1 WHERE id = '$userIdx';
SELECT bar FROM foo WHERE id = '$userIdx';
UNLOCK TABLES;
(quick hack, not tested YMMV)
C.
[Back to original message]
|