title: PHP PDO - Testing connection before doing query?
body: <pre><code>   public function smart_query($query, $options = null, $bindoptions = null)
      {  
  
        // Code to Reconnect incase of timeout
        try {
             $this-&gt;db-&gt;query('SELECT * FROM templates');
            }
        catch (PDOException $e) 
            {  
              echo $e;
  
              $pdooptions = array(
              PDO::ATTR_PERSISTENT    =&gt; true,
              PDO::ATTR_ERRMODE       =&gt; PDO::ERRMODE_EXCEPTION
              );
  
              $this-&gt;db = new PDO("mysql:host=localhost;dbname=$this-&gt;database", "$this-&gt;username", "$this-&gt;password", $pdooptions);
            }
  
        $this-&gt;statement = $this-&gt;db-&gt;prepare($query);
  
        if($bindoptions != null)
          {
            $this-&gt;bind($bindoptions);
          }
  
        $this-&gt;execute();
  
        if($options != null)
         {
           // Return Single Row
           if($options['rows'] == 1)
            {
             return $this-&gt;statement-&gt;fetch(PDO::FETCH_ASSOC);
            }
  
           // Return Multiple Rows
           elseif($options['rows'] != 1)
            {
             return $this-&gt;statement-&gt;fetchAll(PDO::FETCH_ASSOC);      
            } 
         }
      }
  </code></pre>
  
  <p>I've saw this code today, and got really confused.
  It looks like he is trying to process a simple query, before doing the actual query.</p>
  
  <p>Why is he checking if the connection is still open?</p>
  
  <p>I thought that PDO only destroys it's connection upon script finishing executing automatically?
  Is that correct to check if it's open or closed?</p>