pdo - 在非对象上调用成员函数 prepare()

2021-12-26 00:00:00 php pdo

此代码出现错误:

致命错误:在非对象上调用成员函数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).

相关文章