Home  >  Article  >  Backend Development  >  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
Patricia ArquetteOriginal
2024-11-13 04:21:02891browse

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
}
  • Declare $pdo Globally:
global $pdo;

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

Equivalent to mysql_num_rows with PDO:

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

$stmt->rowCount();

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!

Statement:
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