【发布时间】:2020-04-14 11:18:36
【问题描述】:
我正在使用 PHP PDO 准备语句从 MySQL 数据库中获取一些数据。我必须在准备好的语句的执行中使用 if 语句。请参阅下面的代码以更好地理解
$query = "SELECT * FROM my_table WHERE 1=1";
if(isset($_GET['one'])){
$query .= " AND one = :one";
}
if(isset($_GET['two'])){
$query .= " AND two = :two";
}
if(isset($_GET['three'])){
$query .= " AND three = :three";
}
$result = $db->prepare($query);
$result->execute([
/* ------------------------------------
How to declare the above parameters here
as it will show error if any of the if statement is not true?
----------------------------------------*/
]);
我想知道如何在 $result->execute(......]) 块中使用 if 语句声明准备好的数组参数?
【问题讨论】:
标签: php mysql pdo prepared-statement