bcmcfc
bcmcfc

Reputation: 26825

MySQL select and lock in order to update with PHP

I have a PHP script which selects a code from an InnoDB table and dishes it out to a user. Once it's selected it, it goes back and updates the table.

Partial code:

$read = "SELECT code FROM codes WHERE someCondition = true"

Then:

$update = "UPDATE codes SET status = 'assigned', timeAssigned = NOW() WHERE someCondition = true"

How do I go about using InnoDB's locking to do this a bit more robustly and prevent two users getting the same codes?

Upvotes: 2

Views: 2208

Answers (1)

Marc B
Marc B

Reputation: 360882

Relevant docs: here

SELECT FOR UPDATE code FROM codes ...

Upvotes: 4

Related Questions