imimin Posted July 30, 2009 Share Posted July 30, 2009 Is there a way to write a SQL statement with a WHERE clause which contains more than one condition? Such as : SELECT prod_name FROM products WHERE cat=red, blue,yellow Thank you! Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/ Share on other sites More sharing options...
trq Posted July 30, 2009 Share Posted July 30, 2009 SELECT prod_name FROM products WHERE cat IN('red','blue','yellow'); Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886645 Share on other sites More sharing options...
imimin Posted July 30, 2009 Author Share Posted July 30, 2009 Now, is there a way to set this up in a link? Such as : <A HREF="<http://www.anysite.com . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A> Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886891 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 Hey imimin, When passing special characters through the url, they will be converted to the html version of that character. However, when you parse it back in php they will be separated by the delimiter you specified. So the following should do the trick. <?php if(isset($_GET['cat'])){ $cats = explode(",",$_GET['cat']); $qs = ""; foreach($cats as $cat){ $cat = mysql_real_escape_string($cat); $qs .= "'$cat',"; } $qs = substr($qs,0,-1); $query = "SELECT prod_name FROM products WHERE cat IN($qs);"; $run = mysql_query($query); // .... parse query.... } ?> Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886902 Share on other sites More sharing options...
bruce080 Posted July 30, 2009 Share Posted July 30, 2009 Now, is there a way to set this up in a link? Such as : <A HREF="<http://www.anysite.com . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A> Sure, try the link above and use explode with "," as the string to separate the cat variables, (or ", " if there is going to be a space after the commas). Perhaps the following code might work? <?php $catArray = explode(",", $_GET['cat']); $query = "SELECT prod_name FROM products WHERE cat IN('$catArray[0]','$catArray[1]','$catArray[2]');" ?> EDIT: Nevermind, I forgot about the special character thing posted above. Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886906 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 Now, is there a way to set this up in a link? Such as : <A HREF="<http://www.anysite.com . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A> Sure, try the link above and use explode with "," as the string to separate the cat variables, (or ", " if there is going to be a space after the commas). Perhaps the following code might work? <?php $catArray = explode(",", $_GET['cat']); $query = "SELECT prod_name FROM products WHERE cat IN('$catArray[0]','$catArray[1]','$catArray[2]');" ?> EDIT: Nevermind, I forgot about the special character thing posted above. Another reason I wouldn't advise using this model is because you'd be restricted to only 3 categories, that's why you generate the IN portion of the query automatically. Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886912 Share on other sites More sharing options...
imimin Posted July 30, 2009 Author Share Posted July 30, 2009 <?php if(isset($_GET['cat'])){ $cats = explode(",",$_GET['cat']); $qs = ""; foreach($cats as $cat){ $cat = mysql_real_escape_string($cat); $qs .= "'$cat',"; } $qs = substr($qs,0,-1); $query = "SELECT prod_name FROM products WHERE cat IN($qs);"; $run = mysql_query($query); // .... parse query.... } ?> p2grace, are you saying I can use the above code to "receive" the data sent from the link in the format of: <A HREF="<http://www.anysite.com . "?" . "cat=" . "red, blue,yellow">" TITLE="">TEST PAGE</A> ??? Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886924 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 Yeup, sorry I didn't make that more clear. Link to comment https://forums.phpfreaks.com/topic/168112-sql-with-more-than-one-where-condition/#findComment-886932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.