I have an sql query where I want to include an if-then condition. The condition should not simply be built into php so that the code is also safe. I use a prepare Statement.
If the variable $filiale has the value "alle", then all data should be selected. If the variable $filiale has a other value (like "bi), then this value should be searched for in the "frequenz" column and only this fields must be selected. When the variable $filiale hat the value "hsw", only this fields must be selected.
The other WHERE conditions should always be used in the query.
I've read quite a bit, but don't know how to build this into my code.
You can implement this somehow with the SQL command "IF Then" or "CASE" I think. Can anyone help me?
Here is the updated Code:
$filiale = "bi";
if (isset ($filiale)){ $filialauswahl= 'AND filiale = :filiale'; } else {echo '';}
$statement = $pdo->prepare("Select COUNT(*) AS anzahl FROM frequenz WHERE datum BETWEEN :datumvon AND :datumbis AND uhrzeit BETWEEN :uhrzeitvon AND :uhrzeitbis");
$statement->execute(array(':datumvon' => $fdatum_von, ':datumbis' => $fdatum_bis, ':uhrzeitvon' => $fuhrzeit_von, ':uhrzeitbis' => $fuhrzeit_bis, ':filialeauswahl' => $filialauswahl, ':filiale' => $filiale ));
$abfrage = $statement->fetch();
Where ist the error. I become no Message.
question from:
https://stackoverflow.com/questions/65915309/php-sql-query-with-if-parameter 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…