如果在 phpmyadmin 中输入,以下查询将返回所有想要的结果:
SELECT postid, voting
FROM postvotes
WHERE userid = 1
AND postid IN
(1007,1011,1012,1013,1014,
1015,1016,1017,1018,1019,1020,1021,1023,1025,1026,
1027,1028,1029,1030,1031)
但是 PDO 无法 fetchAll()。它只返回第一个匹配项,如 fetch()。
怎么了?
PHP代码:
private function userPostVotings( $postIDs ) {
// $postIDs contains a string like 1,2,3,4,5,6,7...
// generated through implode(',', idArray)
try {
$userPostVote = $this->_db->prepare('SELECT postid, voting
FROM postvotes
WHERE userid = ?
AND postid IN ( ? )');
$userPostVote->setFetchMode(\PDO::FETCH_ASSOC);
$userPostVote->execute( array( $this->_requester['id'], $postIDs ) );
while ( $res = $userPostVote->fetch() ) {
var_dump( $res );
}
} catch (\PDOException $p) {}
}
如果我回显此方法中使用的查询并通过 phpmyadmin 触发它,我会得到正确数量的结果。然而 PDO 只给出了第一个。不管是 fetch() 还是 fetchAll() 循环。