为什么我的pdo变量始终未定义?

时间:2018-01-01 16:09:12

标签: php mysql oop pdo

我有两个php文件,一个是PDO Connection的文件,另一个是在此连接上提交预准备语句的文件:

PDO连接:

class Connection {

protected $user = "ni873420_2sql1";
protected $pass = "#";
public $pdo_log;

public function __construct() {

        try {

            $pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                               $this->user, $this->pass);

        }
        catch (Exception $e) {

            die($e);

        }
    }
}

这是我创建预准备语句的函数:

    public static function getInfobyEmailUsername($argemailusername) {

    $get_info_query = "SELECT * FROM user WHERE email = ? OR username = ?";

    $conn = new Connection();
    $getinfostmt = $conn->pdo_log->prepare($get_info_query);
    $getinfostmt->execute(array($argemailusername, $argemailusername));

    $userinfo = $getinfostmt->fetch(PDO::FETCH_ASSOC);

    if (!empty ($userinfo)) {
        return $userinfo;
    } else {
        return false;
    }

    }

但我一直收到错误:

Fatal error: Call to a member function prepare() on null

我无法在任何地方找到解决方案,任何人都可以帮助我,或者至少发送一个我可以找到解决方案的链接吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

在构造函数中,使用公共变量$pdo_log

  $this->pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', 
                           $this->user, $this->pass);