I have three php scripts on server1, server2, server3.
I have remote mysql server4 with database “watch” – table “mytable” with one column “data”
All these scripts connect to db “watch” table “watch” column “data” and all do the same operation:
- Select first row of column “data”
- Make operation with data
- Delete first row of column “data”
$connect = mysql_connect("ip","user","password") OR DIE("Can't connect "); mysql_select_db("watch") or die("Can't select database"); $xv=1 do { $select = mysql_query("SELECT data FROM mytable LIMIT 1") or die("wrong select"); if (!$select) { echo 'Could not run query: ' . mysql_error(); exit;} $data = mysql_fetch_row($select); ----------- operation with first row of "data" ------------- mysql_query("DELETE FROM mytable LIMIT 1") or die("wrong delete"); } while ($xv++<100);
For example,
first row of “data” contains uniq value “Alex”.
second row of “data” contains uniq value “Michael”.
third row of “data” contains uniq value “George”.
PHP scripts run in cron at the same time but from different servers.
Is it possible that two or all three scripts select the same value of first row
“Alex”?
I would like to avoid that two or three scripts select simultaneously the same value of first row.
Use locking, unlocking of tables.
LOCK TABLESexplicitly acquires table locks for the current client session.UNLOCK TABLESexplicitly releases any table locks held by the current session. Also read this link.