如何检查列值是否为空?示例代码:PHP PDO获取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????
{
...
}
'$ row ['total_rating_votes'];'contains? – 2010-05-18 09:31:41
$ row ['total_rating_votes'];应该包含NULL,因为submission_ratings表中不存在任何记录。我通过在mysqladmin中运行查询来验证这一点。 – Jacob 2010-05-18 09:39:12
如果没有记录,则未设置$ row ['total_rating_votes']。做一个print_r($ row)。您应该检查执行的返回值,这将是找到的记录数。 – 2010-05-18 10:34:17