title: Fatal error: Call to a member function prepare() on a non-object with PDO
body: <p>I have a file that includes another file:</p>
  
  <p>index.php is a dynamic template</p>
  
  <pre><code>&lt;?php
      $db = new PDO("mysql:host=localhost;dbname=db", $DB_USER, $DB_PASSWORD);
      include(functions.php);
  ?&gt;
  </code></pre>
  
  <p>functions.php</p>
  
  <pre><code>&lt;?php
      $preparedStatement = $db-&gt;prepare("SELECT id FROM table LIMIT 1");
      $preparedStatement-&gt;execute();
      $firstId = $preparedStatement-&gt;fetchAll();
  ?&gt;
  </code></pre>
  
  <p>when functions.php executes I get:</p>
  
  <pre><code>Fatal error: Call to a member function prepare() on a non-object
  </code></pre>
  
  <p>at line</p>
  
  <pre><code>$preparedStatement = $db-&gt;prepare("SELECT id FROM table LIMIT 1");
  </code></pre>
  
  <p>What is wrong with this code such that it throws the error?</p>
  
  <p>Edit: The error arose from me connecting directly the the functions.php file, it did not appear when it was included from index.php, the problem I had was content not loading in later inclusions, that was caused by me forgetting to properly address arrays in a later included file. The selected answer is correct for the question I presented.</p>