为什么我的pdo变量始终未定义?(Why is my pdo variable all the time undefined? [duplicate])

编程入门 行业动态 更新时间:2024-10-11 07:29:37
为什么我的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 answers

I 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 null

I 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);

更多推荐

本文发布于:2023-07-30 14:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338837.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   未定义   pdo   variable   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!