title: How to display mysql error while using PDO in a connection class?
body: <p>I am trying to write a small class so I can use it to connect to my databases But I am havinf an issue that PDO is not displaying an error.</p>
  
  <p>What I am trying to do is to display mysql error when the query fails so I know what is the error and fix it.</p>
  
  <p>In my call I have 4 methods where i need to catch mysql errors.</p>
  
  <pre><code>startConnection()
  getOneResult()
  processQuery()
  getDataSet()
  </code></pre>
  
  <p>This is my current class
  Can some one please show me how to display the mysql error. Note I tried to use try catch to catch the error but that did not work for me.</p>
  
  <p>Thanks for your help</p>
  
  <pre><code>&lt;?php
  
  class connection {
  
      private $connString;
      private $userName;
      private $passCode;
      private $server;
      private $pdo;
      private $errorMessage;
  
      private $pdo_opt = array (
                              PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
                              PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC
                              );
  
  
  
      function __construct($dbName, $serverName = 'localhost'){
  
          //sets credentials
          $this-&gt;setConnectionCredentials($dbName, $serverName);
  
          //start the connect
          $this-&gt;startConnection();
  
      }
  
      function startConnection(){
  
  
              $this-&gt;pdo = new PDO($this-&gt;connString, $this-&gt;userName, $this-&gt;passCode, $this-&gt;pdo_opt);
  
              if( ! $this-&gt;pdo){
  
                  $this-&gt;errorMessage  = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
                  $this-&gt;errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
                  echo $this-&gt;getError();
  
              }
  
      }
  
  
      //this will close the PDO connection
      public function endConnection(){
  
          $this-&gt;pdo-&gt;close;
      }
  
      //return a dataset with the results
      public function getDataSet($query, $data = NULL)
      {
          $cmd = $this-&gt;pdo-&gt;prepare( $query );
  
          $cmd-&gt;execute($data);
          return $cmd-&gt;fetchAll();
      }
  
  
      //return a dataset with the results
      public function processQuery($query, $data = NULL)
      {
          $cmd = $this-&gt;pdo-&gt;prepare( $query );
  
          return $cmd-&gt;execute($data);
      }
  
  
      public function getOneResult($query, $data = NULL){
          $cmd = $this-&gt;pdo-&gt;prepare( $query );
          $cmd-&gt;execute($data);
          return $cmd-&gt;fetchColumn();
      }
  
      public function getError(){
          if($this-&gt;errorMessage != '')
              return $this-&gt;errorMessage;
          else
              return true;  //no errors found
  
      }
  
      //this where you need to set new server credentials with a new case statment
      function setConnectionCredentials($dbName, $serv){
  
          switch($serv){
  
              case 'NAME':
                  $this-&gt;connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                  $this-&gt;userName     = 'user';
                  $this-&gt;passCode     = 'password';
              break;
  
              default:
                  $this-&gt;connString   = 'mysql:host=localhost;dbname=rdi_cms;charset=utf8';
                  $this-&gt;userName     = 'user';
                  $this-&gt;passCode     = 'pass!';
              break;
  
              }
  
      }
  
  
  
  }
  
  
  ?&gt;
  </code></pre>