I want to execute a parameterized query to perform a search on user parameters. There are quite a few options, and not all of them will be delivered all the time. How can I make a standard query that sets all possible parameters, but ignore some of these parameters if the user has not selected a meaningful parameter value?
Here is an imaginary example to illustrate what I will do
$sql = 'SELECT * FROM people WHERE first_name = :first_name AND last_name = :last_name AND age = :age AND sex = :sex';
$query = $db->prepare($sql);
$query->execute(array(':first_name' => 'John', ':age' => '27');
Obviously, this will not work, because the number of parameters provided does not match the number of expected parameters. Do I have to process the request every time only the specified parameters are included in the WHERE clause, or is there a way that some of these parameters are ignored or always return true when checking?
source
share