Home > Backend Development > PHP Tutorial > Why am I Getting the 'Call to a member function prepare() on a non-object' Error in PHP?

Why am I Getting the 'Call to a member function prepare() on a non-object' Error in PHP?

Patricia Arquette
Release: 2024-11-13 04:21:02
Original
986 people have browsed it

Why am I Getting the

Solving the "Call to a Member Function prepare()" Error

The error message "Call to a member function prepare() on a non-object" indicates that the $pdo variable is not an object when the prepare() method is being called. This can occur for several reasons.

Possible Cause:

In the provided code, the $pdo variable is undefined within the repetirDados() function. It needs to be passed in as an argument or declared within the global namespace with global $pdo.

How to Fix:

  • Pass $pdo as an Argument:
function repetirDados($email, $pdo) {
    // ... rest of the code
}
Copy after login
  • Declare $pdo Globally:
global $pdo;

function repetirDados($email) {
    // ... rest of the code
}
Copy after login

Equivalent to mysql_num_rows with PDO:

Instead of mysql_num_rows, PDO provides the rowCount() method:

$stmt->rowCount();
Copy after login

The above is the detailed content of Why am I Getting the 'Call to a member function prepare() on a non-object' Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template