Can the OR keyword be used for same field in a mysql table?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Can the OR keyword be used for same field in a mysql table?

Post by drayarms »

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:

Code: Select all

$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?
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: Can the OR keyword be used for same field in a mysql tab

Post by drayarms »

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.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: Can the OR keyword be used for same field in a mysql tab

Post by Gopesh »

Hi,can u pls provide the current working code.
drayarms
Forum Contributor
Posts: 134
Joined: Fri Dec 31, 2010 5:11 pm

Re: Can the OR keyword be used for same field in a mysql tab

Post by drayarms »

@Gopesh. Ok this is what I have in mind. First of all let me show you the form>

Code: Select all

<form>
Seeking A:
							<select name= "gender">
								<option value= "man">Man</option>
								<option value= "woman">Woman</option>
								<option value= "man OR woman">Both</option>
							<s/elect>


Interest;
							<select name= "interest">
								<option value= "dating">Dating</option>
								<option value= "friendship">Friendship</option>								
							</select>   

<input type ="submit" input name="submit" value="Search!"/> 

</form>
And my query looks something like this:

Code: Select all

if(isset($_POST['submit'])) {
            $sql = "SELECT* FROM members WHERE gender= {'$_POST['gender']}' AND interest = {'$_POST['gender']}'   "
}

What do you think?
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: Can the OR keyword be used for same field in a mysql tab

Post by Gopesh »

Hi,the value name "man OR woman" doesnot work any SQL OR function.it is just a name to get the values...
Post Reply