• <tfoot id='xwSsG'></tfoot>
    <legend id='xwSsG'><style id='xwSsG'><dir id='xwSsG'><q id='xwSsG'></q></dir></style></legend>
      <bdo id='xwSsG'></bdo><ul id='xwSsG'></ul>

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

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

        PDO 的 FETCH_INTO $这个类不起作用

        时间:2023-09-19
        <i id='n2L6t'><tr id='n2L6t'><dt id='n2L6t'><q id='n2L6t'><span id='n2L6t'><b id='n2L6t'><form id='n2L6t'><ins id='n2L6t'></ins><ul id='n2L6t'></ul><sub id='n2L6t'></sub></form><legend id='n2L6t'></legend><bdo id='n2L6t'><pre id='n2L6t'><center id='n2L6t'></center></pre></bdo></b><th id='n2L6t'></th></span></q></dt></tr></i><div id='n2L6t'><tfoot id='n2L6t'></tfoot><dl id='n2L6t'><fieldset id='n2L6t'></fieldset></dl></div>
              <tbody id='n2L6t'></tbody>

              <bdo id='n2L6t'></bdo><ul id='n2L6t'></ul>

                • <small id='n2L6t'></small><noframes id='n2L6t'>

                  <legend id='n2L6t'><style id='n2L6t'><dir id='n2L6t'><q id='n2L6t'></q></dir></style></legend>
                  <tfoot id='n2L6t'></tfoot>

                • 本文介绍了PDO 的 FETCH_INTO $这个类不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想使用 PDO 的 FETCH_INTO 的构造函数填充类:

                  I want to populate class with constructor using FETCH_INTO of PDO:

                  class user
                  {
                      private $db;
                      private $name;
                  
                      function __construct($id)
                      {
                          $this->db = ...;
                  
                          $q = $this->db->prepare("SELECT name FROM users WHERE id = ?");
                          $q->setFetchMode(PDO::FETCH_INTO, $this);
                          $q->execute(array($id));
                  
                          echo $this->name;
                      }
                  }
                  

                  这不起作用.没有错误,只是没有.脚本没有错误,FETCH_ASSOC 工作正常.

                  This does not work. No error, just nothing. Script has no errors, FETCH_ASSOC works fine.

                  FETCH_INTO 有什么问题?

                  推荐答案

                  您的代码中有两个错误:

                  You have two errors in your code:

                  1) 你忘记了 $q->fetch()

                  1) You forgot $q->fetch()

                   ...
                   $q->execute(array($id));
                   $q->fetch(); // This line is required
                  

                  2) 但即使在添加 $q->fetch() 之后你也会得到这个:

                  2) But even after adding $q->fetch() you'll get this:

                  致命错误:无法访问私有属性 User::$name in ...

                  Fatal error: Cannot access private property User::$name in ...

                  因此,如您所见,即使在类方法内部调用 PDO,它也无法访问私有成员.

                  So, as you can see, PDO cannot access private members even if it is called inside class method.

                  这是我的解决方案:

                  ...
                  $q->execute(array($id));
                  $q->setFetchMode(PDO::FETCH_ASSOC);
                  $data = $q->fetch();
                  foreach ($data as $propName => $propValue)
                  {
                      // here you can add check if class property exists if you don't want to
                      // add another properties with public visibility
                      $this->{$propName} = $propValue;
                  }
                  

                  这篇关于PDO 的 FETCH_INTO $这个类不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:PHP Laravel:无法建立连接,因为目标机器主动拒绝 下一篇:PDO 返回错误“找不到驱动程序"使用已知的

                  相关文章

                  最新文章

                    <bdo id='LlXTB'></bdo><ul id='LlXTB'></ul>

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

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

                    <tfoot id='LlXTB'></tfoot>