Wie überprüfen Sie, ob ein Spaltenwert null ist? Beispielcode: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????
{
...
}
Was bedeutet '$ row [ 'total_rating_votes'];' enthalten? –
$ row ['total_rating_votes']; sollte NULL enthalten, da in der Tabelle submission_ratings keine Datensätze vorhanden sind. Ich habe dies überprüft, indem ich die Abfrage in mysqladmin ausgeführt habe. – Jacob
Wenn keine Datensätze vorhanden sind, wird $ row ['total_rating_votes'] nicht gesetzt. Mach eine print_r ($ Zeile). Sie sollten den Rückgabewert von execute überprüfen, der die Anzahl der gefundenen Datensätze angibt. –