Is there a way to make a param optional in PHP PDO?

RonOntoto

New Member
Say I have this query:\[code\]$qry = 'SELECT p.id FROM products p WHERE p.type = :type AND p.parent = :parent';\[/code\]If I wanted to make "type" optional then the only way I know how to do it would be to do something like this:\[code\]$qry = 'SELECT p.id FROM products p WHERE p.parent = :parent';if(isset($type)){ $qry .= 'AND p.type = :type';}\[/code\]Is this the best way? I am wondering if there is a way I can keep it like the original, and use some sort of parameter to indicate that it is optional... For example if $type = "*" then pull all types.Whats the best way to do this?
 
Back
Top