对于该代码,任何人都可以为我提供更好的解决方案:
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->beginTransaction();
$dbh->exec("LOCK TABLES test2 WRITE");
$row = $dbh->query('SELECT * from test2 WHERE c > 0 LIMIT 1');
$stmt = $dbh->prepare("UPDATE test2 SET c=c-1 WHERE a=:a and c>0");
$stmt->bindParam(':a', $row['a']);
$stmt->execute();
/**
...
....
.....
**/
$dbh->exec("UNLOCK TABLES");
$dbh->commit();
$dbh = null;
} catch (PDOException $e) {
error_log("Error!: " . $e->getMessage() . "\n", 3, "./my-errors.log");
exit();
}
当我同时连接到该脚本时,每个连接都应该在表 test2(字段 A)中有自己的行。
感谢您的想法:-)