I am trying to work out the most effective way of structuring a piece of code for a multipart search with optional criteria:
e.g. a car search:
Make
Model
Engine
Each item is optional so you can select make and engine and leave model blank or just select make or model.
A prepared statement expects a set number of parameters within the bind_param which would cause issue if for instance someone doesn't select a make as make would ='' returning false for all result which have a make selected.
what would be the most efficient way to either structure my query or php code so I don't have to write a separate query for each possible outcome or have a large collection of 'if' conditions.
Starting code:
if ($stmt = $this->db->prepare("SELECT * FROM `cars` WHERE make=?, model=?, engine=?"))
{
$stmt->bind_param('i', $make, $model, $engine);
$stmt->execute();
$stmt->bind_result($this->id, $this->name, $this->make, $this->model, $this->engine, $this->doors);
while ($stmt->fetch())
{
$entry[] = $this->objectToArray();
}
return $entry;
}
thanks in advance.