php - MySQL WHERE IN () + AND , PDO returns only one row -


following query returns wanted results if entered in phpmyadmin:

select     postid, voting        postvotes       userid = 1  ,        postid in                     (1007,1011,1012,1013,1014,                    1015,1016,1017,1018,1019,1020,1021,1023,1025,1026,                    1027,1028,1029,1030,1031) 

but pdo fails fetchall(). returns first match fetch().

what's wrong?

php code:

private function userpostvotings( $postids ) {  // $postids contains string 1,2,3,4,5,6,7... // generated through implode(',', idarray)    try {      $userpostvote = $this->_db->prepare('select postid, voting      postvotes      userid = ?      , postid in ( ? )');      $userpostvote->setfetchmode(\pdo::fetch_assoc);     $userpostvote->execute( array( $this->_requester['id'], $postids ) );      while ( $res = $userpostvote->fetch() ) {           var_dump( $res );       }    } catch (\pdoexception $p) {}  } 

if echo out query used in method , fire through phpmyadmin correct number of results. pdo gives first. no matter if loop fetch() or fetchall().

it not pdo's fetchall() of course, query.

which not

in (1007,1011,1012,1013,1014) 

but

in ('1007,1011,1012,1013,1014') 

and of course find first value string cast first number

one have create query placeholders representing every array member, , bind array values execution:

$ids = array(1,2,3); $stm = $pdo->prepare("select * t id in (?,?,?)"); $stm->execute($ids); 

to make query more flexible, it's better create string ?s dynamically:

$ids = array(1,2,3); $in  = str_repeat('?,', count($arr) - 1) . '?'; $sql = "select * table column in ($in)"; $stm = $db->prepare($sql); $stm->execute($ids); $data = $stm->fetchall(); 

Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -