如何检查列值是否为空?示例代码:
$db = DBCxn::getCxn();
$sql = "SELECT exercise_id, author_id, submission, result, submission_time, total_rating_votes, total_rating_values
FROM submissions
LEFT OUTER JOIN submission_ratings ON submissions.exercise_id=submission_ratings.exercise_id
WHERE id=:id";
$st = $db->prepare($sql);
$st->bindParam(":id", $this->id, PDO::PARAM_INT);
$st->execute();
$row = $st->fetch();
$this->total_rating_votes = $row['total_rating_votes'];
if($this->total_rating_votes == null) // this doesn't seem to work even though there is no record in submission_ratings????
{
...
}
答案 0 :(得分:13)
当您连接到数据库时,您可以设置一些属性来控制PDO在数据库查询返回时处理Null和空字符串的方式
PDO :: setAttribute(PDO :: ATTR_ORACLE_NULLS,$ option)
其中$ option是以下之一:
答案 1 :(得分:2)
这不是你想做的吗?
foreach($row as $r){
if($r->total_rating_votes == null){
//do something
}
其实您可能想尝试:
if($r->total_rating_votes == ""){/*do something*/}
因为php可能已经将null值转换为空字符串,然后它实际上不是null,所以它是“”
希望这有帮助!
答案 2 :(得分:0)
感谢您的所有答案。经过一些实验,这段代码解决了我的问题
$this->total_rating_votes = $row['total_rating_votes'];
if(!isset($this->total_rating_votes)) // this is now true if this record had a NULL value in the DB!!!
{
...
}