stefffff Posted February 17, 2009 Share Posted February 17, 2009 Hello, I am using PHP to create some filter and i have a SELECT problem... I have a table with 2 fields: id_filter, id_product how can i make a selection like this: SELECT id_product FROM filters WHERE id_filter='5' and id_filter='7' Is it possible to use multiple values for id_filter in the same query? because i have products that have 3 filters values applied to them and i want to select them by that certain filters! is there a multiple mysql select function that i'm missing? any ideas would be grat... thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/145564-is-it-possible-to-select-like-this/ Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 Yes here solution <?php $idfilters = array (1,3,5,7) foreach ($idfilters as $idfilter) { $dbquery = mysql_query("SELECT id_product FROM filters WHERE id_filter= '$idfilter'") or die ("Error in select query") ; } ?> other solution too use or SELECT id_product FROM filters WHERE id_filter= 2 or id_filter=3 Quote Link to comment https://forums.phpfreaks.com/topic/145564-is-it-possible-to-select-like-this/#findComment-764191 Share on other sites More sharing options...
Mchl Posted February 17, 2009 Share Posted February 17, 2009 I'd recommend using single query, instead of running a query in a loop. Also check this syntax SELECT id_product FROM filters WHERE id_filter IN('5','7') Quote Link to comment https://forums.phpfreaks.com/topic/145564-is-it-possible-to-select-like-this/#findComment-764269 Share on other sites More sharing options...
premiso Posted February 17, 2009 Share Posted February 17, 2009 Expanding on what Mchl said: <?php $idfilters = array (1,3,5,7) $idfilters = implode(", ", $idfilters); $query = mysql_query("SELECT id_product FROM filters WHERE id_filter IN($idfilters)") or die ("Error in select query") ; ?> A tad bit easier and a ton less MySQL server load. Quote Link to comment https://forums.phpfreaks.com/topic/145564-is-it-possible-to-select-like-this/#findComment-764280 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.