为什么我的pdo变量始终未定义?(Why is my pdo variable all the time undefined? [duplicate])
这个问题在这里已经有了答案:
PDO致命错误:调用成员函数prepare()上的非对象 3答案 参考:什么是变量作用域,哪些变量可以从哪里访问,什么是“未定义变量”错误? 3个答案 调用成员函数prepare()上的非对象PHP Help 8的答案我有两个php文件,一个是PDO连接的文件,另一个是准备好的语句在此连接上提交的文件:
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我无法找到任何解决方案,任何人都可以帮助我或至少发送一个链接,我可以找到解决方案?
谢谢!
This question already has an answer here:
PDO Fatal error: Call to a member function prepare() on a non-object 4 answers Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers Call to a member function prepare() on a non-object PHP Help 8 answersI have two php files, one is a file for a PDO Connection and one is a file in which a prepared statement is submitted on this connection:
PDO Connection:
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); } } }And this is the function where I create the prepared statement:
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; } }But I keep getting the error:
Fatal error: Call to a member function prepare() on nullI cant find a solution anywhere, can anyone help me with this or at least send a link where I can find the solution?
Thank you!
最满意答案
在你的构造函数中,使用公共变量$pdo_log
$this->pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', $this->user, $this->pass);In your construct function, use the public variable $pdo_log
$this->pdo_log = new PDO('mysql:host=localhost;dbname=ni873420_2sql1', $this->user, $this->pass);更多推荐
发布评论