当前位置:   article > 正文

mysql from in pdo_MySQL WHERE IN()AND,PDO只返回一行

mysql where in pdo

如果在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()的循环 .

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/139361
推荐阅读
相关标签
  

闽ICP备14008679号