title: PDO intermittent error "Call to a member function execute() on a non-object"
body: <p>So I am getting this intermittent error on a call to PDO::execute(). Error is "Call to a member function execute() on a non-object". It happens at line 117 in my db class. Here is my class below. Page loads in fine and then intermittently when I refresh this error comes back.</p>
  
  <pre><code>class db extends PDO {
      private $error;
      private $sql;
      private $bind;
      private $errorCallbackFunction;
      private $errorMsgFormat;
  
      public function __construct($dsn, $user="", $passwd="") {
          $options = array(
              PDO::ATTR_PERSISTENT =&gt; false, 
              PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
          );
  
          try {
              parent::__construct($dsn, $user, $passwd, $options);
          } catch (PDOException $e) {
              $this-&gt;error = $e-&gt;getMessage();
          }
      }
  
      private function cleanup($bind) {
          if(!is_array($bind)) {
              if(!empty($bind)) {
                  $bind = array($bind);
              }
              else {
                  $bind = array();
              }
          }
          return $bind;
      }
  
      public function run($sql, $bind="") {
          $this-&gt;sql = trim($sql);
          $this-&gt;bind = $this-&gt;cleanup($bind);
          $this-&gt;error = "";
  
          try {
              $pdostmt = $this-&gt;prepare($this-&gt;sql);
              if($pdostmt-&gt;execute($this-&gt;bind) !== false) {
                  if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this-&gt;sql)) {
                      return $pdostmt-&gt;fetchAll(PDO::FETCH_ASSOC);
                  }
                  elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this-&gt;sql)) {
                      return $pdostmt-&gt;rowCount();
                  }
              }   
          } catch (PDOException $e) {
              $this-&gt;error = $e-&gt;getMessage();    
              $this-&gt;debug();
              return false;
          }
      }
  
      public function select($table, $where="", $bind="", $fields="*") {
          $sql = "SELECT " . $fields . " FROM " . $table;
          if(!empty($where)) {
              $sql .= " WHERE " . $where;
          }
          $sql .= ";";
  
          return $this-&gt;run($sql, $bind);
      }
  
  }   
  </code></pre>
  
  <p>Now whenever I change PDO::ATTR_PERSISTENT to false the problem goes away so it looks to be an issue with my construct call running over itself.</p>
  
  <p>Also here is my my model that the error is showing up on.
                  
  
  <pre><code>class QuickView extends Model {
  
  function getProduct($sku) {
      $bind = array(
          ":sku" =&gt; "$sku"
      );
      $result = $this-&gt;Quickview-&gt;db-&gt;select('`PRODUCTS`', 'PR_SKU = :sku', $bind, 'PR_SKU, PR_URLofImage, PR_UnitPrice, PR_Description');
  
      return $result;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And then here is the class it extends.</p>
  
  <pre><code>class Model extends DB {
      protected $_model;
  
      function __construct() {
  
          global $inflect;
  
          $this-&gt;db = new DB("mysql:host=localhost;dbname=dbname", "username", "password");
          $this-&gt;_limit = PAGINATE_LIMIT;
          $this-&gt;_model = get_class($this);
          $this-&gt;_table = strtolower($this-&gt;_model)."s";
      }
  
      function __destruct() {
      }
  
  }
  </code></pre>