Alechko Posted July 18, 2012 Share Posted July 18, 2012 Hi folks, I need some help with taking vars from array to the DB. my array something like this: Array ( [0] => 137 [1] => 800 [2] => 732 [3] => 198) The Array contains IDs of products in database. I want to do something like this first: $db->Query("SELECT catID FROM `products` WHERE `id` = '$VAROFARRAY'"); That the DB Query will loop the whole array variables. And then: $db->Query("SELECT `name` FROM `categories` WHERE `id= '$catID'"); I a little bit complicated with it.. thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/265882-from-array-to-sql-query/ Share on other sites More sharing options...
ignace Posted July 18, 2012 Share Posted July 18, 2012 Something like this: $strarray = implode(',', array_filter($VAROFARRAY, 'intval')); $sql = " SELECT name FROM categories C JOIN products P ON P.catID = C.id WHERE P.id IN($strarray) "; Quote Link to comment https://forums.phpfreaks.com/topic/265882-from-array-to-sql-query/#findComment-1362368 Share on other sites More sharing options...
Alechko Posted July 18, 2012 Author Share Posted July 18, 2012 thanks, but maybe you didn't get me. I have already the array of my products. All I have to is to loop it on the query of categories to get the name. Array => 1,3,5,7,8 (The ID's of the products) and I want something like: forearch($array as pID) ... while($row = $db->Query("SELECT FROM `cats` WHERE `id` = '{$pID}'") .. Something liike that.. thank you very much... Quote Link to comment https://forums.phpfreaks.com/topic/265882-from-array-to-sql-query/#findComment-1362552 Share on other sites More sharing options...
xyph Posted July 18, 2012 Share Posted July 18, 2012 That's what he's doing, but he's doing it in one query. He's also making sure they're integers before putting them in the query. SELECT whatever FROM cats WHERE id IN (1,2,3,5,7, Will select rows where id is 1,2,3,5,7 or 8 Quote Link to comment https://forums.phpfreaks.com/topic/265882-from-array-to-sql-query/#findComment-1362558 Share on other sites More sharing options...
Alechko Posted July 19, 2012 Author Share Posted July 19, 2012 ah okey, thanks! I get it now! Quote Link to comment https://forums.phpfreaks.com/topic/265882-from-array-to-sql-query/#findComment-1362649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.