我有一个像这样的 SQL 查询:
$stmt = $dbh->prepare("SELECT userID FROM User WHERE areaCode=? AND contactNo=?");
$stmt->bindValue(1, $areaCode ? $areaCode : null, PDO::PARAM_INT);
$stmt->bindValue(2, $contactNo, PDO::PARAM_INT);
$stmt->execute();
当 $areaCode 为 null
时,此查询将失败,因为 areaCode=null
需要使用 areaCode IS NULL
进行测试。是否仍然可以参数化此类查询?
最佳答案
您可以通过 <=> 做到这一点(即参数化)运算符(operator):
$stmt = $dbh->prepare("SELECT userID FROM User WHERE areaCode <=> ? AND contactNo=?");
关于php - 在 PDO 查询的 where 子句中处理可能的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18766698/