I am building a dynamic news application for a site. The news will be split up into categories which may then contain subcategories(which may contain subcategories) and news articles. I have created a recursive function to return all of the category IDs for all subcategories of a certain category. I would like to run a single query to grab all of the news posts whose category is in the set that was returned by the recursive function.
When I try to use this (I'M USING PEAR DB)
//$myReturnedIDs is a comma delimited list of ids. 5,6,7,8,10,12
$oPrep = $oConn->prepare("SELECT NewsID FROM SiteNews WHERE NewsCategory IN (?)");
$oRes = $oConn->execute($oPrep, array($myReturnedIDs));
The resulting query looks like this:
SELECT NewsID FROM SiteNews WHERE NewsCategory IN('5,6,7,8,10,12')
while I need it to look like:
SELECT NewsID FROM SiteNews WHERE NewsCategory IN(5,6,7,8,10,12)
Now, I realize I could populate that portion of the query in the prepare statement, but I feel that leads to some possible hole for an attack. Am I being over cautious, since the data is not user generated? Is there some way to make this work? My other thought was to add a ? for each ID, amount would be determined before the prepare statement (after getting all the IDs), but that feels clunky to me. Any suggestions?