drayarms Posted May 24, 2011 Share Posted May 24, 2011 I'm trying to create a search form where members of a site can search other members whose user information is stored in a mysql database. One of the search parameters is gender which searches for records from a column in the database called gender where members can either have the value "man" or "woman". The user can choose between 3 options from a pull down menu named gender (man, woman, both). Then I'm thinking about using a select query to search the database upon submission of the form, which goes something like this: $sql= SELECT* FROM members WHERE gender ='{$_POST['gender']}' ; Now I can easily assign the value "man" for the option man in the pull down menu and "woman for the option woman, to match their corresponding names in the database. The problem lies with the "both" option which has to be either man or woman. I'm thinking about assigning it the value "man OR woman" so that when the form is submitted, the query would read: SELECT*FROM members WHERE gender ='{$_POST[man OR woman']}; I just don't know if this would be a right usage of the OR keyword and if such a query would work. Before trying it out, I'd like to know if this makes any sense and if not, what's an alternative way to work around this? Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/ Share on other sites More sharing options...
Pikachu2000 Posted May 24, 2011 Share Posted May 24, 2011 If 'both' is selected, there is no need to even compare it. Just leave the WHERE clause out completely. Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/#findComment-1219786 Share on other sites More sharing options...
xyph Posted May 24, 2011 Share Posted May 24, 2011 Pikachu's answer is the correct one. If this comes up again with more than two options, and you want to select, say, 2/x SELECT `data` FROM `table` WHERE `row`='compare' OR `row`='compare' Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/#findComment-1219791 Share on other sites More sharing options...
drayarms Posted May 24, 2011 Author Share Posted May 24, 2011 @Pikachu Well I forgot to mention that there are other search parameters besides gender. Take for example lets say I had to search for gender AND interest. That compels me to specify the WHERE clause for both columns right? With that in mind, how would I handle this? Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/#findComment-1219798 Share on other sites More sharing options...
xyph Posted May 24, 2011 Share Posted May 24, 2011 SELECT `data` FROM `table` WHERE ( `col`='foo' OR `col`='bar' ) AND `hello`='world' Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/#findComment-1219800 Share on other sites More sharing options...
Pikachu2000 Posted May 25, 2011 Share Posted May 25, 2011 Build the query string dynamically. Check the values coming from the form and construct it conditionally, based on those. Here's a start: $query = 'SELECT `field` FROM `table`'; $where = array(); if( $_POST['gender'] != 'both' ) { $where[] = mysql_real_escape_string($_POST['gender']); } if( !empty($_POST['interest']) ) { $where[] = mysql_real_escape_string($_POST['interest']); } if( count($where) > 0) { $query .= ' WHERE ' . implode(' AND ', $where); } Quote Link to comment https://forums.phpfreaks.com/topic/237374-can-the-or-keyword-be-used-for-same-field-in-a-mysql-table/#findComment-1219841 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.