I am using InnoDB and have the following table
officeRechNr
year | month | id |
------------------------
2016 | 7 | 2 |
2016 | 6 | 5 |
2016 | 5 | 6 |
My script works as follows:
So, if the script would run one after another, I would expect:
New id is 3
New id is 4
New id is 5
I suggested that this behaves differently when I execute a parallel script.
Here is my script:
$db = new mysqli("localhost","user","pass","db");
$year = date("Y");
$month = date("m");
$stmt = $db->prepare('SELECT zahl FROM officeRechNr WHERE jahr = ? AND monat = ?');
$stmt->bind_param('ii', $year, $month);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$number = $row['zahl'] + 1;
sleep(20);
$stmt = $db->prepare('UPDATE officeRechNr set zahl = ? WHERE jahr = ? and monat = ?');
$stmt->bind_param('iii',$number, $year, $month);
$stmt->execute();
echo "New id is $number";
I ran both scripts at the same time, so I assumed that $ number should be 3 for both scripts, and then they sleep for 20 seconds. Therefore i would expect
New id is 3
New id is 3
to my surprise, the result was
New id is 3
New id is 4
, , mysql php.
? script - ?