<small id='A3HtC'></small><noframes id='A3HtC'>

      • <bdo id='A3HtC'></bdo><ul id='A3HtC'></ul>
      <i id='A3HtC'><tr id='A3HtC'><dt id='A3HtC'><q id='A3HtC'><span id='A3HtC'><b id='A3HtC'><form id='A3HtC'><ins id='A3HtC'></ins><ul id='A3HtC'></ul><sub id='A3HtC'></sub></form><legend id='A3HtC'></legend><bdo id='A3HtC'><pre id='A3HtC'><center id='A3HtC'></center></pre></bdo></b><th id='A3HtC'></th></span></q></dt></tr></i><div id='A3HtC'><tfoot id='A3HtC'></tfoot><dl id='A3HtC'><fieldset id='A3HtC'></fieldset></dl></div>
      <tfoot id='A3HtC'></tfoot><legend id='A3HtC'><style id='A3HtC'><dir id='A3HtC'><q id='A3HtC'></q></dir></style></legend>

    1. 未定义变量:pdo,在 null 上调用成员函数 prepare(

      时间:2023-10-05

    2. <tfoot id='DeWn2'></tfoot>

      <small id='DeWn2'></small><noframes id='DeWn2'>

          • <bdo id='DeWn2'></bdo><ul id='DeWn2'></ul>

                <tbody id='DeWn2'></tbody>
              • <i id='DeWn2'><tr id='DeWn2'><dt id='DeWn2'><q id='DeWn2'><span id='DeWn2'><b id='DeWn2'><form id='DeWn2'><ins id='DeWn2'></ins><ul id='DeWn2'></ul><sub id='DeWn2'></sub></form><legend id='DeWn2'></legend><bdo id='DeWn2'><pre id='DeWn2'><center id='DeWn2'></center></pre></bdo></b><th id='DeWn2'></th></span></q></dt></tr></i><div id='DeWn2'><tfoot id='DeWn2'></tfoot><dl id='DeWn2'><fieldset id='DeWn2'></fieldset></dl></div>
                <legend id='DeWn2'><style id='DeWn2'><dir id='DeWn2'><q id='DeWn2'></q></dir></style></legend>

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

                问题描述

                我正在关注一个视频并仔细检查所有代码,一切似乎都一样,但我遇到了这些错误.

                I was following a video and double checking all code and everything seems to be the same yet I get these errors.

                错误:

                注意:未定义变量:第 14 行 QueryBuilder.php 中的 pdo

                致命错误:在 QueryBuilder.php 第 14 行调用成员函数 prepare() 为 null

                QueryBuilder.php:

                class QueryBuilder
                {
                    protected $pdo;
                
                    public function __construct($pdo)
                    {
                        $this->pdo = $pdo;
                    }
                
                    public function selectAll($table)
                    {
                        $query = $pdo->prepare("SELECT * FROM `$table`"); // --> LINE 14 <--
                        $query->execute();
                        return $query->fetchAll();
                    }
                }
                

                Connection.php:

                class Connection
                {
                    public static function make()
                    {
                        $servername = "localhost";
                        $dbUsername = "root";
                        $dbPassword = "";
                        $dbName = "test";
                
                        try {
                            $pdo = new PDO("mysql:host=$servername;dbname=$dbName", $dbUsername, $dbPassword);
                            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
                            return $pdo;
                        }
                        catch(PDOException $e){
                            die($e->getMessage());
                        }
                    }
                }
                

                init.php:

                require "database/Connection.php";
                require "database/QueryBuilder.php";
                require "app/Product.php";
                
                $query = new QueryBuilder(Connection::make());
                

                推荐答案

                如评论中所述,在 OOP 中,您需要使用 $this->pdo 为其传递对象的属性,而不是变量 $query = $pdo-> ,因为你已经在:

                As stated in comments, in OOP, you need to use $this->pdo passing the object's property for it, instead of the variable $query = $pdo-> since you've construct'ed it in:

                public function __construct($pdo)
                {
                    $this->pdo = $pdo;
                    ^^^^^^^^^^
                }
                

                即:

                $query = $this->pdo->prepare
                

                这篇关于未定义变量:pdo,在 null 上调用成员函数 prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:PDO 和 MySQL IN/NOT IN 查询的正确格式 下一篇:PDO 未捕获异常 - 插入值列表与列列表不匹配

                相关文章

                最新文章

                1. <i id='6wJhH'><tr id='6wJhH'><dt id='6wJhH'><q id='6wJhH'><span id='6wJhH'><b id='6wJhH'><form id='6wJhH'><ins id='6wJhH'></ins><ul id='6wJhH'></ul><sub id='6wJhH'></sub></form><legend id='6wJhH'></legend><bdo id='6wJhH'><pre id='6wJhH'><center id='6wJhH'></center></pre></bdo></b><th id='6wJhH'></th></span></q></dt></tr></i><div id='6wJhH'><tfoot id='6wJhH'></tfoot><dl id='6wJhH'><fieldset id='6wJhH'></fieldset></dl></div>
                    <bdo id='6wJhH'></bdo><ul id='6wJhH'></ul>
                2. <tfoot id='6wJhH'></tfoot>
                  <legend id='6wJhH'><style id='6wJhH'><dir id='6wJhH'><q id='6wJhH'></q></dir></style></legend>

                  1. <small id='6wJhH'></small><noframes id='6wJhH'>