title: MySQL PHP lock table, is there a better solution when expacting simultaneously connections
body: <p>has any one a better solution for me for that code:</p>
  
  <pre><code>&lt;?php
  
  try {
      $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $password);
  
      $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $dbh-&gt;beginTransaction();
  
      $dbh-&gt;exec("LOCK TABLES test2 WRITE");
  
      $row = $dbh-&gt;query('SELECT * from test2 WHERE c &gt; 0 LIMIT 1');
  
      $stmt = $dbh-&gt;prepare("UPDATE test2 SET c=c-1 WHERE a=:a and c&gt;0");
      $stmt-&gt;bindParam(':a', $row['a']);
      $stmt-&gt;execute();
  
      /**
      ...
      ....
      .....
      **/
  
      $dbh-&gt;exec("UNLOCK TABLES");
  
      $dbh-&gt;commit();
      $dbh = null;
  } catch (PDOException $e) {
      error_log("Error!: " . $e-&gt;getMessage() . "\n", 3, "./my-errors.log");
      exit();
  }
  </code></pre>
  
  <p>When i get simultaneously connections to that script, every connection should have his own row from the table test2 (field A).</p>
  
  <p>Thanks for your ideas :-)</p>