php - PDO Excute with multiple OR -


below code using dynamically load news articles web page. $dbconn->execute fires correctly when passing single
where (select * newsitem subcategoryid=12 order posted desc)
but fails return result when passing

(select * newsitem subcategoryid=1 || subcategoryid=12 || subcategoryid=27 || subcategoryid=27 || subcategoryid=28 || subcategoryid=29 || subcategoryid=30 || subcategoryid=31 || subcategoryid=32 || subcategoryid=43 order posted desc) 

this code behind generates above queries:

`$strhtml = ""; $strhtml .= "";

    if(strpos($params['subcatid'], "|") < 0){         $q = "select * newsitem subcategoryid=".$params['subcatid']." order posted desc";     } else {         $q = "select * newsitem ";         $idarr = explode("|", $params['subcatid']);          for($i = 0; $i < count($idarr); $i++){             $q .= "subcategoryid=".$idarr[$i]." || ";         }         $q = substr($q, 0, -4)." order posted desc";     }     return $q;      $pstmt = $dbconn->prepare($q);     $pstmt->execute();      for($i = 0; $i < $params['numarts']; $i++){         $row = $pstmt->fetch(pdo::fetch_assoc);         if($i == 0){             $startart = $row['newsitemid'];         }         $strhtml .= "<a href='javascript:cnews.get(".$row['newsitemid'].")'>".$row['headline']."</a><br />";     }     $numpages = ceil($pstmt->rowcount() / 10);     $strhtml .= "<div id='articlenav' style='font-weight: bold; padding-top: 5px; width: 50%;'>page: 1 of ".$numpages."<img src='/files/images/sitewide/next.gif' style='float: right;''></div>";     $strhtml .= "<div id='startarticleid' style='visibility: hidden;'>".$startart."</div></div><div id='newsarticle' style='padding-top: 20px;'>&nbsp;</div>";     //return $params['id']." ".$params['subcatid'];     return $strhtml;` 

when running function $params['subcatid'] passed either subcatid='12' or pipe separated list subcatid='1|12|26|27|28|29|30|31|32|44'. query generated, prepared, executed. stated when there 1 subcategoryid results returned expected; no results returned when multiple or subcategoryid passed.

i tested copying , pasting queries database directly , both return results expected.

what happening when passing complex query?

as have been told in comments, multiple ors can substituted handy in operator.

yet not easy make prepared statements. here go:

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

$ids = explode("|", $params['subcatid']); $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

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -