我怎样才能select * from users where gender = ?;
并且性别与'f'或'm'相同?或者<?p>
我正在尝试创建一个动态的select语句,在那里我可以回来
我能够用前两个但不是第三个来做。用mysql可以吗?
答案 0 :(得分:1)
我假设您正在使用PDO and prepared statements:
$pdo = new PDO($dsn, $user, $pass, $options);
$gender_defined = $gender == 'f' || $gender == 'm';
if ($gender_defined) {
$query = $pdo->prepare("select * from table where gender = ?");
$query->bindParam(1, $gender, PDO::PARAM_STR);
$query->execute();
} else {
$query = $pdo->query("select * from table where gender = 'f' or gender = 'm'");
}
$result = $query->fetchAll(PDO::FETCH_ASSOC);