title: PHP PDO Exceptions
body: <p><br>
  I have a problem which I couldn't solve for a long time. I have a simple DB class like this one</p>
  
  <pre><code>class DB {
  
      private static $dbserver = "mysql:dbname=db;host=localhost";
      private static $dbuser = "root";
      private static $dbpass = "";
  
      public static function connectDB() {
  
          @$dbh = new PDO(self::$dbserver, self::$dbuser, self::$dbpass);
          $dbh-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
          $dbh-&gt;setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
  
          return $dbh;
      }
  </code></pre>
  
  <p>And a Service Class with methods to write data in the DB. The type of the tables is InnoDB. The Service Class looks like this</p>
  
  <pre><code>function insert_data($data) {
      $error = NULL;
      try {
          $dbh = DB::connectDB();
          $dbh-&gt;beginTransaction();
  
          $query_insert_1 = 'INSERT INTO table (f1, f2, f3) VALUES (?,?,?)';
          $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $stmt = $dbh-&gt;prepare($query_insert_1);
          $stmt-&gt;execute(array($d1, $d2, $d3));
  
          $last_insert_id = $dbh-&gt;lastInsertId();
  
          $query_insert_2 = 'INSERT INTO table2 (f4, f5, f6) VALUES (?,?,?)';
          $stmt = $dbh-&gt;prepare($query_insert_2);
          $stmt-&gt;execute(array($last_insert_id, $d4, $d5));
  
          $dbh-&gt;commit();             
      }
      catch(Exception $e) {                       
          $dbh-&gt;rollback();
          $error = $e-&gt;getMessage();
      }
      $return = new Result($error ? 1 : 0, $error ? 0 : 1, $error ? $error : '', NULL);
      error_log(print_r($error,1));
      return $return;
  }
  </code></pre>
  
  <p>PDO only throws an Exception if I set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION IN the function insert_data. If I set it in the DB class no Exception will be thrown. What is the reason for this behaviour?</p>
  
  <p>I have the solution to throw an Exception when I set the constant in the function from this question (<a href="https://stackoverflow.com/questions/12872601/pdo-exception-is-not-being-thrown">PDO exception is not being thrown</a>)</p>
  
  <p>Thanks for help</p>
  
  <p>Michael</p>
  
  <p>EDIT:
  My originally code is the following</p>
  
  <pre><code>class CarService {
  
  private $dbh;
  
  function __construct($dbh) {
      $this-&gt;dbh = $dbh;
  }
  
  function insert_car($customer_id, $brand, $model) {
      $error = NULL;
  
      try {
          $this-&gt;dbh-&gt;beginTransaction();
  
          $query_insert_car = 'INSERT INTO fahrzeug (kunde_id, fabrikat, modell) VALUES (?,?,?)';
  
          $stmt = $this-&gt;dbh-&gt;prepare($query_insert_car);
          $stmt-&gt;execute(array($customer_id, $brand, $model));
          // Get the ID of the last inserted car
          $last_insert_id = $this-&gt;dbh-&gt;lastInsertId();
          // Create the entry of the corresponding car version
          $query_insert_car_version = 'INSERT INTO fahrzeugversion (fahrzeug_id, version, fabrikat, modell) VALUES (?,?,?,?)';
          $stmt = $this-&gt;dbh-&gt;prepare($query_insert_car_version);
          $stmt-&gt;execute(array($last_insert_id, 1, $brand, $model));
  
          $this-&gt;dbh-&gt;commit();               
      }
      catch(Exception $e) {                       
          $this-&gt;dbh-&gt;rollback();
          $error = $e-&gt;getMessage();
      }
  
      $return = new Result(1, 0, NULL, NULL);
  error_log(print_r($error,1));
      return $return;
  }
  </code></pre>
  
  <p>new_car.php</p>
  
  <pre><code>&lt;?php
  
  require_once('DB.php');
  require_once('CarService.php');
  require_once('Result.php');
  
  $dbh = DB::connectDB();
  $car_service = new CarService($dbh);
  
  $result = new Result(1, 0, NULL, NULL);
  
  if (isset($_POST['fabrikat']) &amp;&amp; isset($_POST['modell'])) {
      $brand = trim($_POST['fabrikat']);
      $model = trim($_POST['modell']);
  
      $result = $car_service-&gt;insert_car(1, $brand, $model);          
  }   
  print_r(json_encode($result));
  ?&gt;
  </code></pre>
  
  <p>DB.php</p>
  
  <pre><code>&lt;?php
  
  class DB {
  
      private static $dbserver = "mysql:dbname=db;host=localhost";
      private static $dbuser = "root";
      private static $dbpass = "";
  
      public static function connectDB() {
  
          $dbh = new PDO(self::$dbserver, self::$dbuser, self::$dbpass);
          $dbh-&gt;setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
          $dbh-&gt;setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
          $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          return $dbh;
      }
  
  }
  ?&gt;
  </code></pre>