SELECT statement with a wildcard
Posted: Wed Jul 28, 2004 11:11 am
Hello. I'm trying to make a select statement based on user input. My dillema is this: there may or may not be user input for a particular variable. The user has the option to search (for example) by category, by username, by both, or by no parameters (lists all results). The way I figured I'd do it is this:
So if $_GET['category'] or $_GET['username'] are unset, I want MySQL to pull all the results. I tried both the * and the %, but to no avail. What is the wildcard symbol?
Thanks!
Code: Select all
<?php
if (isset($_GET['category'])) {
$category = $_GET['category']; }
else {
$category = //wildcard here, because i want results from all categories
}
if (isset($_GET['username'])) {
$username = $_GET['username']; }
else {
$username = //same dillema
}
//finally, the query
$query = "SELECT * FROM `table` WHERE `category` = $category AND `username`= $username AND /*more variables */";
?>Thanks!