functions.php?php $preparedStatement = $db->prepare("SELECT" name="description" /> functions.php?php $preparedStatement = $db->prepare("SELECT" />
28510

Fatal error: Call to a member function prepare() on a non-object with PDO

Question:

I have a file that includes another file:

index.php is a dynamic template

<?php $db = new PDO("mysql:host=localhost;dbname=db", $DB_USER, $DB_PASSWORD); include(functions.php); ?>

functions.php

<?php $preparedStatement = $db->prepare("SELECT id FROM table LIMIT 1"); $preparedStatement->execute(); $firstId = $preparedStatement->fetchAll(); ?>

when functions.php executes I get:

Fatal error: Call to a member function prepare() on a non-object

at line

$preparedStatement = $db->prepare("SELECT id FROM table LIMIT 1");

What is wrong with this code such that it throws the error?

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.

Answer1:

inclusion of files is wrong;

include index.php on function.php page only , dont include vice versa

best practice to use include_once

Recommend

  • TSQL: Prevent trigger suppressing error but rolling back transaction
  • Issues with unchecked generics
  • PreparedStatement - how specify to use default value of column
  • Passing inputs to program prompt in a batch file
  • PHP PDO Update prepared statement problem
  • Bundle install fails - Gem using github url - What is wrong?
  • Insert Path of a file with \\\\ in mysql using java
  • Doctrine: UniqueEntity with Many-to-Many field
  • Inserting NULL/empty string using libpqxx library
  • Error processing multiple files
  • Eloquent paginate function in Slim 3 project using twig
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Using $this when not in object context
  • Submit form in a displaytag pagination
  • output of program is not same as passed argument
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Statically linking a C++ library to a C# process using CLI or any other way
  • How to limit post in wp_query
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Delete MySQLi record without showing the id in the URL
  • Why winpcap requires both .lib and .dll to run?
  • Unanticipated behavior
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?