Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

PHP Error: Call to a Member Function prepare() on a Non-Object

The error points first to the value before ->prepare(), not necessarily to the SQL. Trace how that receiver was initialized and passed to the failing line.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

This error means PHP tried to call prepare() on a value that was not the object the code expected. In $db->prepare($sql), inspect $db first—not the SQL. The failing line shows where the invalid call happened; the cause may be an earlier failed initialization, missing function scope, overwritten value, or a framework type mismatch.

What the error means

In PHP, ->prepare() calls a method on the expression to its left. The exact error, “Call to a member function prepare() on a non-object,” identifies that receiver as the immediate problem. It does not by itself prove that the database is down, that credentials are wrong, or that the query is malformed.

The receiver could be a database handle such as $pdo or $mysqli, but prepare() is not exclusive to database APIs. For example, Yii’s BaseDataProvider::prepare() prepares data models and keys (Yii BaseDataProvider API). Use the stack trace and the expression before ->prepare() to identify which object the code expected.

Trace the receiver before changing the SQL

  1. Find the failing call. Read the complete stack trace, open the file and line it identifies, and note the exact expression before ->prepare().
  2. Inspect its runtime value. At that point in execution, determine whether the receiver is null, false, an array, an unexpected object, or an uninitialized property. Use a debugger or a temporary diagnostic that reports type and null status; do not log credentials or sensitive query parameters.
  3. Follow the value backward. Check its constructor, factory, include, dependency-injection configuration, and each conditional branch that could skip initialization or overwrite the value. Find the first point where it stops being the expected object.
  4. Check how the dependency reaches this code. In a plain function, a variable created outside that function is not automatically a local variable inside it. Pass the handle as an argument or inject it into an object instead of relying on unexplained global state.
  5. Check framework expectations. If the trace points into a framework component, compare the value supplied by your controller or configuration with the documented type expected by that component.
  6. Correct the cause and retest the affected path. Make the smallest change supported by what you found, then exercise the code in the application’s normal environment.

Common causes and what to check

The database handle was never initialized

A connection factory or constructor may fail, a configuration branch may not run, or an assignment may leave the variable as null or false. Check the connection-creation code and its first warning or exception rather than assuming the later prepare() call explains why initialization failed.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The handle is out of scope

A PHP Q&A example of this exact error involved a database handle that was unavailable inside a function; the suggested correction was to pass the handle into the function (scope-related PHP/MySQLi example). Treat that as a diagnostic example, not a universal fix: verify the variable’s value where your call runs, then pass dependencies explicitly or inject them.

The value was replaced with the wrong type

Look for assignments that reuse the receiver’s variable or property for another purpose. If the runtime value is an array, string, or unrelated object, trace the assignment that changed it and give values with different roles distinct names.

A framework component received the wrong kind of data

In a June 2014 Yii 2 forum report, a list view failed because the controller supplied an array of models where the view expected a data-provider object. The accepted reply showed constructing and passing a SqlDataProvider (Yii forum example). This correction applies when the stack trace and property contract match that case—not to a PDO or MySQLi connection call.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Distinguish an invalid receiver from a failed prepare

These are separate failure points. The title’s error is about the value receiving the method call. If that value is a valid database object, its own prepare() operation may still fail because of the query, driver, schema, permissions, or connectivity. PDO documents that preparation can fail and that behavior depends on error handling; MySQLi can return failure or report/throw an error under its configured reporting behavior (PDO::prepare; MySQLi::prepare). Inspect the earliest database warning or exception and use it to investigate that separate problem.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When a valid PDO connection reaches prepare(), use placeholders for input values. The PHP manual says: “Use these parameters to bind any user-input, do not include the user-input directly in the query.” Placeholders represent complete data literals; they cannot substitute for table names, column names, or arbitrary SQL fragments (PDO::prepare manual). Placeholder mistakes matter after the receiver is valid; they do not explain a non-object receiver.

Choose the fix that matches the receiver

What the trace and runtime value show Where to investigate Appropriate next step
Database variable is null, false, or unset Connection setup, constructor, configuration, or a skipped branch Find why setup did not produce the expected handle; inspect its first error.
Database variable is unavailable inside a function Scope and dependency handoff Pass the handle as a parameter or inject it into the object.
Receiver is an array or unrelated object in a framework call Framework property contract and controller-to-view data flow Supply the documented object type; for the cited Yii case, that means a data provider rather than a model array.
Receiver is a valid PDO or MySQLi object, but preparation fails First driver warning or exception, SQL template, schema, permissions, and connectivity as indicated Diagnose the database preparation failure separately from the non-object error.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.