做
return $statement->fetchColumn() > 0;
將評估一個表達式,檢查是列大於零更大,所以你得到返回的布爾true
/false
;如果計數大於零,則爲true
,否則爲false
。
如果你只是返回$statement->fetchColumn()
而不檢查> 0
,你會得到確切的計數而不是布爾值。由於PHP是弱/弱類型,表現形式,例如1 == true
是真的*,您可以使用結果if語句太像這樣
public function checkForeignKey($aId) {
$statement = $this->connection->prepare("SELECT COUNT(*) FROM cursusleerling WHERE cursusid = ?");
$statement->bindValue(1, $aId);
$statement->execute();
return $statement->fetchColumn(); // removed > 0
}
/* Class defined and such above, using it below */
$obj = new YourClass(..); /* We only see a method of your class, not the whole thing, so just using some psudocode */
$count_result = $obj->checkForeignKey(1337);
if ($count_result) {
echo "The result was ".$count_result;
} else {
echo "No results returned";
}
*而在強類型語言中,1 == true
是假的。你可以得到一個嚴格比較1 === true
(三級等式),這是在PHP
你想知道到底是什麼的NUM? –
那麼:$ result = $ statement-> fetchColumn();然後返回$ result; ?我不是以答覆的形式發帖,因爲我不是100%確定的。 – IRGeekSauce
它將返回所選行的數量。 –