title: InnoDB Select For Update
body: <p>If I have a simple statement as such for an InnoDB Table</p>
  
  <pre><code>UPDATE &lt;table&gt; SET locked=1, col2=&lt;Val2&gt; WHERE locked=0
  </code></pre>
  
  <p>Is there any need to really do SELECT FOR UPDATE?</p>
  
  <p>Since InnoDB supports row level locking is there any chance of a collision even thousands of clients are executing the same script simultaneously? </p>
  
  <p>Update:</p>
  
  <p>Something like this would prevent locks</p>
  
  <pre><code>$dbh = new PDO(DSN, DB_USER, DB_PASS);
  $dbh-&gt;beginTransaction();
  $selQuery = $dbh-&gt;prepare("SELECT &lt;col&gt; FROM &lt;table&gt; WHERE status=0 LIMIT 1 FOR UPDATE");
  $selQuery-&gt;bindColumn(&lt;col1&gt;, $col1);
  $selQuery-&gt;execute();
  
  $selQuery-&gt;fetch(PDO::FETCH_BOUND);
  
  $dbh-&gt;query("UPDATE &lt;table&gt; SET status=1 WHERE status=0 LIMIT 1");
  
  $dbh-&gt;commit();
  </code></pre>