ricmetal Posted February 10, 2012 Share Posted February 10, 2012 hi all. how can i do something like SELECT * FROM table WHERE column_1 = if(the value to use is 'a', use 'a' as the search value, else if the value is 'z', then select all the rows; select rows that have a,b,c..z)? (this is MySQLi)regards Quote Link to comment Share on other sites More sharing options...
fenway Posted February 12, 2012 Share Posted February 12, 2012 Forget the syntax. Describe your use case. Quote Link to comment Share on other sites More sharing options...
ricmetal Posted February 12, 2012 Author Share Posted February 12, 2012 i have a few html select elements. they all have items like cat, dog, bear (categorized by type: animals, car brands, etc) and one item 'all'. when the user selects a specific item from a select, like dog, i need to retrieve the dogs, but if the user selects the all item (to include dogs, cats, bears), i need to retrieve all animals. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted February 12, 2012 Share Posted February 12, 2012 I would do something like this: <?php $where = ""; if($_POST['category'] != "all"){ $id = (int)$_POST['category']; $where = " and category = $id"; } $sql = mysqli_query($resource, "select * from my table where 1 $where"); ?> Quote Link to comment Share on other sites More sharing options...
ricmetal Posted February 13, 2012 Author Share Posted February 13, 2012 yeah, i had to do something like that, because depending on the amount of where clauses i needed to set the same amount of variables for the fetched information to be stored in. im using MySQLi. i don't remember how MySQL worked in order to fetch the data from the query into the variables, but..anyway, unless there's some other way to get the data fetched into the variables (in MySQLi) using a dynamic WHERE if query then i guess manually is the way to go. regards Quote Link to comment 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.