在获取PDO :: FETCH_OBJECT的同时,预见到不同的行为(While and foreach different behaviour with fetching PDO::FETCH_OBJECT)

使用类似的东西:

$db = new PDO(connection_details) $query = "SELECT * FROM vservers LIMIT 5"; $result = $db->query($query);

然后尝试用fe获取记录

while ($row = $result->fetchAll(PDO::FETCH_OBJ)) { var_dump($row); }

它返回一个带有StdObjects的数组,如下所示:

array (size=5) 0 => object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=14) public 'template' => string 'debian-7.0-x86' (length=14) 1 => object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=33) public 'template' => string 'ubuntu-15.04' (length=27)

并且使用foreach它返回StdObjects

foreach ($result->fetchAll(PDO::FETCH_OBJ) as $key) { var_dump($key); } object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=6) public 'template' => string 'debian' (length=6) object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=6) public 'template' => string 'ubuntu' (length=6)

有人可以解释一下这种行为吗? 通常情况下,我foreach一样返回对象,但这是一个好习惯吗?

While using something like:

$db = new PDO(connection_details) $query = "SELECT * FROM vservers LIMIT 5"; $result = $db->query($query);

And then try to get records with while f.e.

while ($row = $result->fetchAll(PDO::FETCH_OBJ)) { var_dump($row); }

it returns an array with StdObjects like this:

array (size=5) 0 => object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=14) public 'template' => string 'debian-7.0-x86' (length=14) 1 => object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=33) public 'template' => string 'ubuntu-15.04' (length=27)

And with foreach it returns StdObjects

foreach ($result->fetchAll(PDO::FETCH_OBJ) as $key) { var_dump($key); } object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=6) public 'template' => string 'debian' (length=6) object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=6) public 'template' => string 'ubuntu' (length=6)

Can someone please explain this behaviour? Normally, I would like to return Objects like with foreach, but is it a good practice ?

最满意答案

fetchAll()以数组形式返回所有结果,其中每个元素都是一个表示表中行的对象。

在while代码中,第一次迭代将$row设置$row整个结果集,并将其作为单个数组转储。 只有一次迭代,因为下一次调用fetchAll()返回一个空数组,因为没有什么可以提取的。

在你的foreach代码中, fetchAll()将数组返回给foreach ,然后一次迭代一个元素,为每个对象设置$key 。 然后你将一个物体转储到你的身体里。

通常在使用fetch()时使用, while不是fetchAll() 。 此代码将等同于foreach :

while ($key = $result->fetch(PDO::FETCH_OBJ)) { var_dump($key); }

fetchAll() returns all the results as an array, where each element is an object that represents a row from the table.

In your while code, the first iteration sets $row to the entire result set, and dumps it as a single array. There's only one iteration because the next call to fetchAll() returns an empty array, because there's nothing left to fetch.

In your foreach code, fetchAll() returns the array to foreach, which then iterates over it one element at a time, setting $key to each object. Then you dump that one object in your body.

Normally when you're using while you use fetch(), not fetchAll(). This code will be equivalent to the foreach:

while ($key = $result->fetch(PDO::FETCH_OBJ)) { var_dump($key); }在获取PDO :: FETCH_OBJECT的同时,预见到不同的行为(While and foreach different behaviour with fetching PDO::FETCH_OBJECT)

使用类似的东西:

$db = new PDO(connection_details) $query = "SELECT * FROM vservers LIMIT 5"; $result = $db->query($query);

然后尝试用fe获取记录

while ($row = $result->fetchAll(PDO::FETCH_OBJ)) { var_dump($row); }

它返回一个带有StdObjects的数组,如下所示:

array (size=5) 0 => object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=14) public 'template' => string 'debian-7.0-x86' (length=14) 1 => object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=33) public 'template' => string 'ubuntu-15.04' (length=27)

并且使用foreach它返回StdObjects

foreach ($result->fetchAll(PDO::FETCH_OBJ) as $key) { var_dump($key); } object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=6) public 'template' => string 'debian' (length=6) object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=6) public 'template' => string 'ubuntu' (length=6)

有人可以解释一下这种行为吗? 通常情况下,我foreach一样返回对象,但这是一个好习惯吗?

While using something like:

$db = new PDO(connection_details) $query = "SELECT * FROM vservers LIMIT 5"; $result = $db->query($query);

And then try to get records with while f.e.

while ($row = $result->fetchAll(PDO::FETCH_OBJ)) { var_dump($row); }

it returns an array with StdObjects like this:

array (size=5) 0 => object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=14) public 'template' => string 'debian-7.0-x86' (length=14) 1 => object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=33) public 'template' => string 'ubuntu-15.04' (length=27)

And with foreach it returns StdObjects

foreach ($result->fetchAll(PDO::FETCH_OBJ) as $key) { var_dump($key); } object(stdClass)[3] public 'vserverid' => string '898' (length=3) public 'templatename' => string 'Debian' (length=6) public 'template' => string 'debian' (length=6) object(stdClass)[4] public 'vserverid' => string '792' (length=3) public 'templatename' => string 'Ubuntu' (length=6) public 'template' => string 'ubuntu' (length=6)

Can someone please explain this behaviour? Normally, I would like to return Objects like with foreach, but is it a good practice ?

最满意答案

fetchAll()以数组形式返回所有结果,其中每个元素都是一个表示表中行的对象。

在while代码中,第一次迭代将$row设置$row整个结果集,并将其作为单个数组转储。 只有一次迭代,因为下一次调用fetchAll()返回一个空数组,因为没有什么可以提取的。

在你的foreach代码中, fetchAll()将数组返回给foreach ,然后一次迭代一个元素,为每个对象设置$key 。 然后你将一个物体转储到你的身体里。

通常在使用fetch()时使用, while不是fetchAll() 。 此代码将等同于foreach :

while ($key = $result->fetch(PDO::FETCH_OBJ)) { var_dump($key); }

fetchAll() returns all the results as an array, where each element is an object that represents a row from the table.

In your while code, the first iteration sets $row to the entire result set, and dumps it as a single array. There's only one iteration because the next call to fetchAll() returns an empty array, because there's nothing left to fetch.

In your foreach code, fetchAll() returns the array to foreach, which then iterates over it one element at a time, setting $key to each object. Then you dump that one object in your body.

Normally when you're using while you use fetch(), not fetchAll(). This code will be equivalent to the foreach:

while ($key = $result->fetch(PDO::FETCH_OBJ)) { var_dump($key); }