MYSQL Select/Update Lock Question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
tbrown1
Forum Newbie
Posts: 17
Joined: Wed Oct 25, 2006 10:58 am

MYSQL Select/Update Lock Question

Post by tbrown1 »

Ok the answer to this quesiton is probably obvious but it is bothering me.
So I am wiriting a script that many people will be adding data to at the same time.
The script basically runs a select statement get a row of data take one cell out then adds a value to the value from that cell and then runs an update statment to put that new data in the database.
My worry is what happens if two people do this at the same time and there select statments both run before one of the users update statement the second person will then be adding to a smaller number then they should be and messing up the data.
Is this a concern or is there automatic locking in php and mysql that will not allow this to happen.
If there isn't should I use something like LOCK TABLE X WRITE or something to that effect?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

tbrown1 wrote:The script basically runs a select statement get a row of data take one cell out then adds a value to the value from that cell and then runs an update statment to put that new data in the database.
Probably you do not need to select the value.

Code: Select all

UPDATE tablename SET fieldname=fieldname+4711 WHERE id=1234
tbrown1
Forum Newbie
Posts: 17
Joined: Wed Oct 25, 2006 10:58 am

Post by tbrown1 »

Thanks that worked
Post Reply