How do I conditionally add a clause to my query in PHP?

assam_siddibapa

New Member
In my search form I have, value male, value female, and value both. If you select both, I do not wish to have at WHERE the "sex = '$sex'"Should i do it like this:\[code\]if(empty($sex)){ // value empty if you chose "both"$query = "SELECT firstname, lastname, id, user_name, sex, last_access, bostadsort FROM users WHERE (firstname LIKE '%$firstname%' OR lastname LIKE '%$lastname%')";}else{$query = "SELECT firstname, lastname, id, user_name, sex, last_access, bostadsort FROM users WHERE (firstname LIKE '%$firstname%' OR lastname LIKE '%$lastname%') AND sex = '$sex'";}\[/code\]Or is there a smart way to write this?
 
Back
Top