pdo - 在非对象上调用成员函数 prepare() [英] pdo - Call to a member function prepare() on a non-object

查看:22
本文介绍了pdo - 在非对象上调用成员函数 prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码出现错误:

致命错误:在非对象上调用成员函数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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆