. Advertisement .
..3..
. Advertisement .
..4..
I get an error:
Fatal error: Call to a member function prepare() on a non-object in
C:\Users\fel\VertrigoServ\www\login\validation.php on line 42
when I try to run the following code:
function repetirDados($email) {
if(!empty($_POST['email'])) {
$query = "SELECT email FROM users WHERE email = ?";
$stmt = $pdo->prepare($query); // error line: line 42
$email = mysql_real_escape_string($_POST['email']);
$stmt->bindValue(1, $email);
$ok = $stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($results == 0) {
return true;
} else {
echo '<h1>something</h1>';
return false;
}
}
}
How to fix the call to a member function prepare() on a non-object. Please give me some good ideas.
The cause:
The error ”call to a member function prepare() on a non-object” happens in your program because
$pdo
is not determined. You don’t declare it in the function, and it isn’t considered as an argument.Solution:
To solve this problem, you need to specify
$pdo
in the area of global namespace so that it is always available to your function by adding global$pdo
at the top (bad), or you can move it in (good).It is possible to create a function for a database connection on the same page as php and call it whenever you like. As,
You can also write the database connection line on that page whenever you need it. As,