title: check for duplicate entry vs use PDO errorInfo result
body: <p>I have a MySQL table which has a field for email addresses which is defined as unique.  For this example, let's say that all my form does is allow a user to insert their email address into the table.</p>
  
  <p>Since the email field is unique, the query should fail should they try to enter the same email twice.  I'm curious about the trade-offs between between the two scenarios:</p>
  
  <p>1)  Run a quick <code>SELECT</code> statement before performing the insert.  If the select returns results, inform the user, and do not run the <code>INSERT</code> statement.</p>
  
  <p>2)  Run the <code>INSERT</code> statement, and check for a duplicate entry error</p>
  
  <pre><code>// snippet uses PDO
  if (!$prep-&gt;execute($values))
  {
      $err = $prep-&gt;errorInfo();
      if (isset($err[1]))
      {
          // 1062 - Duplicate entry
          if ($err[1] == 1062)
              echo 'This email already exists.';
      }
  }
  </code></pre>
  
  <p>Also, please assume normal use, meaning that duplicate entries should be minimal.  Therefore, in the first scenario you obviously have the overhead of running an additional query for <strong>every</strong> insert, whereas in the second you're relying on error handling.  </p>
  
  <p>Also, I'm curious to hear thoughts on coding style.  My heart says 'Be a defensive programmer!  Check the data before you insert!' while my brain says 'Hmmm, maybe it's better to let MySQL take care of checking the data for you'.</p>
  
  <p><strong>EDIT</strong> - Please note this isn't a "How do I do this" question, but rather a "Why should I do this a particular way" question.  The little code snippet I included works, but what I'm curious about is the best way to solve the problem.</p>