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

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

问题描述

此代码出现错误:

致命错误:在非对象中调用成员函数prepare() 第42行的C:\ Users \ fel \ VertrigoServ \ www \ login \ validation.php

Fatal error: Call to a member function prepare() on a non-object in C:\Users\fel\VertrigoServ\www\login\validation.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天全站免登陆