Recommended Free Tools
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
- Find the failing call. Read the complete stack trace, open the file and line it identifies, and note the exact expression before
->prepare(). - 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. - 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.
- 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.
- 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.
- 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.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problems#1 Best Overall
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.
Rank #2
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.
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.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
Quick Recap
Rank #4
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.




