PHP PDO: Do the fetch styles FETCH_CLASS and FETCH_INTO fetch into private object properties?

前端 未结 4 767
我在风中等你
我在风中等你 2020-12-17 23:06

Pretty short question, here is an example:

$prepared = $this->pdo->prepare(\"SELECT * FROM Users WHERE ID = :ID\");
$statement = $prepared->execute(         


        
4条回答
  •  隐瞒了意图╮
    2020-12-17 23:46

    Very short answer: Yes it will.

    class Foo
    {
        private $id;
        public function echoID()
        {
            echo $this->id;
        }
    }
    $result = $statement->fetchAll(PDO::FETCH_CLASS, "Foo");
    $result[0]->echoID(); // your ID
    

    Aside:

    This will cause syntax errors $statement->fetchAll(PDO::FETCH_INTO, $User);. You can't use FETCH_INTO with the fetchAll method.

提交回复
热议问题