title: PHP PDO error handling halting execution
body: <p>I am pretty new to PHP, coming from ASP, and am having a hard time understanding exactly how error handling works with PHP and PDO.  Here is what I have going on...</p>
  
  <p>PDO is set with:  </p>
  
  <pre><code>PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
  </code></pre>
  
  <p>I am making an AJAX call to a PHP page to process some data and save it to MySQL using PDO.  I have my DB scripts in a try catch like so:</p>
  
  <pre><code>function processStuff(){
      try {
         $sth = $db-&gt;prepare( query );
         $sth-&gt;execute();
      } catch (PDOEXCEPTION $e){
          //log error
      }
  
      //if no error
      return array(some json);
  }
  </code></pre>
  
  <p>let's say something bad happens and I get an error like:</p>
  
  <pre><code>Error: exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'field' cannot be null' in C:\filepath:202
  </code></pre>
  
  <p>What is happening is that the error is halting the script, thereby causing the calling page to just hang.  The AJAX is waiting for a JSON reponse that will tell whether it succeeded or failed.  But because it is hanging, it doesn't get the response, so I don't have the opportunity to show the user there was an error.</p>
  
  <p>I also have a custom error handler function:</p>
  
  <pre><code>function errorHandler($errno, $errstr, $errfile, $errline) {
      //log errors
  }
  
  set_error_handler("errorHandler")
  </code></pre>
  
  <p>that can log errors.  But, due to my newbieness, I am not sure how to use that in the try/catch.</p>
  
  <p>So my question is, how do I log an error when a PDO exception occurs and make sure that the script continues processing so that the calling page with the AJAX doesn't hang.</p>
  
  <p>All the other answers around similar questions assume a level of knowledge that I don't seem to have yet.  I am not clear if I am supposed to throw an error to be caught, or if it is automatically being thrown.  If PDO is automatically throwing it, how do I call my error handler in the catch?  would I just use:</p>
  
  <pre><code>errorHandler();
  </code></pre>
  
  <p>And if I switched to ERRMODE_SILENT, which I believe would continue the processing, how do I detect the error in order to throw it to the catch?</p>
  
  <p>I am sure there are some basic concepts that I am just not understanding about how this works. Code is great, but any explanation is greatly appreciated.</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE:
  Here is the actual try block:</p>
  
  <pre><code>        try {
              $sth = $dbh-&gt;prepare ("INSERT INTO email_validation (email, val_code) VALUES (:a1, :a2)");
              $sth-&gt;bindParam (":a1", $email);
              $sth-&gt;bindParam (":a2", $gencode);
              $sth-&gt;execute ();
          } catch (PDOEXCEPTION $e) {
              $err = true;
              $errmsg = date("F j, Y, g:i a") . "\n" . ERR_URL . "\n" . $e . "\n\n";
              error_log($errmsg,3,ERR_LOG_PATH_INT);
          }
  </code></pre>
  
  <p>The AJAX looks like this:</p>
  
  <pre><code>    $.post('Functions/AJAX-new-account.php', $('form').serialize(), function(data) {
          var c = data.check;
          if(c==1){
              //process client side stuff here
          }else{
              //----Ooops there was an error-----//
              //show error to client
          };
          return false;
      },"json");
  </code></pre>
  
  <p>I guess the JSON is getting written, but I am not understanding why I am not appearing to get to }else{ in my AJAX and the page appears to be stalled.</p>
  
  <p>ANOTHER UPDATE:  so I just tried </p>
  
  <pre><code>trigger_error("blah blah");
  </code></pre>
  
  <p>in the catch block and it stopped hanging.  The errorHandler seems to have processed the error correctly, although my AJAX processed it incorrectly, but it did stop hanging.  Is this a correct way to handle this, or is there a better way?</p>