問題描述
此代碼出現錯誤:
致命錯誤:在非對象上調用成員函數prepare()C:UsersfelVertrigoServwwwloginvalidation.php 第 42 行
Fatal error: Call to a member function prepare() on a non-object in C:UsersfelVertrigoServwwwloginvalidation.php on line 42
代碼:
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;
}
}
}
可能的原因是什么?另一個問題,mysql_num_rows
等價于什么?抱歉,我是 pdo 的新手
What is the possible cause? Another question, What is the equivalent to mysql_num_rows
? sorry, I am newbie with pdo
推薦答案
$pdo
未定義.您沒有在函數內部聲明它,也沒有將其作為參數傳入.
$pdo
is undefined. You're not declaring it inside the function, and it isn't being passed in as an argument.
您需要將它傳入(好),或者在全局命名空間中定義它,并通過將 global $pdo
放在頂部(不好)使其可用于您的函數.
You need to either pass it in (good), or define it in the global namespace and make it available to your function by placing global $pdo
at the top (bad).
這篇關于pdo - 在非對象上調用成員函數 prepare()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!