[英]PHP fetchAll(PDO::FETCH_ASSOC) returns null in statement
[英]PHP PDO fetch null
如何检查列值是否为空? 示例代码:
$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????
{
...
}
连接到数据库时,可以设置一些属性来控制PDO在数据库查询返回时如何处理Null和Empty Strings
PDO :: setAttribute(PDO :: ATTR_ORACLE_NULLS,$ option)
其中$ option是以下之一:
不是你想做的事吗?
foreach($row as $r){
if($r->total_rating_votes == null){
//do something
}
其实你可能想尝试:
if($r->total_rating_votes == ""){/*do something*/}
因为php可能已经将null值转换为空字符串,然后它实际上不是null,所以它是“”
希望这可以帮助!
谢谢你的所有答案。 经过一些实验,这段代码解决了我的问题
$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!!!
{
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.