what is the best way to construct an SQL statement with PDO when it depends on whether some PHP variable are set?
Here is an example;
$query="SELECT * FROM table ";
if($variable1 != "") { $query = $query . "WHERE variable1 = :variable1"; }
if($variable2 != "") { $query = $query . " AND variable2 = :variable2"; }
$query -> execute(array(':variable1' => $variable1, ':variable2' => $variable2));
I have a lot of these if
statements and when binding the variables to the query I don't want to go through all these if
statements again.
Is there an easier way to construct an SQL statement with such if/else conditions?