Just prepare a statement (https://www.php.net/manual/de/pdo.prepared-statements.php) and add where conditions when needed.
And if you have special cases, check for them at the beginning of your code and let a nother method handle this cases. So you will rech a cleaner code.