title: PHP/PDO Data access layer query error: “Fatal error: Call to a member function fetch() on a non-object”
body: <p>I am writing a web application to use CRUD (Create, Retrieve, Update and Delete) methods in a class to reduce code redundancy.  When I try to run the invoking code I keep getting a fatal error that my <code>$res</code> is a non-object. Why is my <code>$res</code> not being picked up as a valid object?</p>
  
  <h3>Invoking Code</h3>
  
  <pre><code>$db = Database::getInstance($username, $password);
  $exists = $db-&gt;tableExists($dbname, $tablename);
  
  echo ($exists)? "TRUE" : "FALSE";
  </code></pre>
  
  <h3>Database Class</h3>
  
  <pre><code>class Database
  {
      private static $instance;
  
      private $dbh;
  
      private static $dbengine = 'mysql';
      private static $dbname = 'database';
      private static $dbhost = 'localhost';
  
  
      function __construct($username, $password)
      {
          $dbname = Database::$dbname;
          $dbhost = Database::$dbhost;
          $dbengine = Database::$dbengine;
          $this-&gt;dbh =  new PDO("$dbengine:host=$dbhost;dbname=$dbname", $username, $password);
       }
  
  
      public static function getInstance($username, $password)
      {
          if (!isset(self::$instance))
          {
              $object = __CLASS__;
              self::$instance = new $object($username, $password);
          }
          return self::$instance;
      }
  
  
      public function tableExists($table)
      {
          $dbname = self::$dbname;
          $sql = $this-&gt;dbh-&gt;prepare("SHOW TABLES FROM :dbname like :table");
          $sql-&gt;bindParam(':dbname' , $dbname);
          $sql-&gt;bindParam( ':table', $table);
          $res = $sql-&gt;execute();
          $rows = $res-&gt;fetch(PDO::FETCH_NUM);
          if ($rows)
          {
              if ($rows[0] == 1)
              {
                  return TRUE;
              }
              return FALSE;
          }
          return FALSE;
      }
  </code></pre>
  
  <p>}</p>
  
  <h3>Code Modified from the Following Link</h3>
  
  <p><a href="http://code.tutsplus.com/tutorials/real-world-oop-with-php-and-mysql--net-1918" rel="nofollow">http://code.tutsplus.com/tutorials/real-world-oop-with-php-and-mysql--net-1918</a></p>
  
  <p>When I run the Invoking Code I get:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function fetch() on a non-object</p>
  </blockquote>