Home > Article > Backend Development > Why am I Getting the "Call to a member function prepare() on a non-object" Error in PHP?
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:
function repetirDados($email, $pdo) { // ... rest of the code }
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!