我有这个代码:
...
$query->setFetchMode(PDO::FETCH_INTO, $this);
$query->execute();
$data = array();
while ($row = $query->fetch()) {
$data[] = $row;
}
return $data;
如果我把var_dump放在这里:
var_dump($data);
return $data;
结果如下:
array (size=3)
0 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
1 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
2 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
如果我把var_dump放在这里:
while ($row = $query->fetch()) {
$data[] = $row;
var_dump($data);
}
结果如下:
array (size=1)
0 =>
object(ChatRoom)[4]
public 'id' string '1' (length=1)
...
array (size=2)
0 =>
object(ChatRoom)[4]
public 'id' string '2' (length=1)
...
1 =>
object(ChatRoom)[4]
public 'id' string '2' (length=1)
...
array (size=3)
0 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
1 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
2 =>
object(ChatRoom)[4]
public 'id' string '3' (length=1)
...
我尝试了 fetchAll 但结果是一样的。我做错了什么?
您正在尝试使用不需要的功能。
$query->setFetchMode(PDO::FETCH_INTO, $this);
删除此行 ^