Php 如何使用fetchColumn()?
这可以返回true或false,但如何检查该值?如何操作Php 如何使用fetchColumn()?,php,Php,这可以返回true或false,但如何检查该值?如何操作 public function checkForeignKey($aId) { $statement = $this->connection->prepare("SELECT COUNT(*) FROM cursusleerling WHERE cursusid = ?"); $statement->bindValue(1, $aId); $statement->execute();
public function checkForeignKey($aId) {
$statement = $this->connection->prepare("SELECT COUNT(*) FROM cursusleerling WHERE cursusid = ?");
$statement->bindValue(1, $aId);
$statement->execute();
return $statement->fetchColumn() > 0;
}
将计算为一个表达式,检查列是否大于零,因此返回一个布尔值true
/false
true
如果计数大于零,则false
否则
如果只返回$statement->fetchColumn()
,而不选中>0
,则将返回精确的计数,而不是布尔值。由于PHP是弱类型/松散类型的,诸如1==true
之类的表达式是true*,因此也可以在if语句中使用结果,如下所示
return $statement->fetchColumn() > 0;
*在强类型语言中,1==true
为false。您可以得到一个严格的比较1===true
(三个等式),这在PHP中是false
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";
}
此行用于检查列计数,因此返回true或false
return $statement->fetchColumn() > 0;
它返回列数您想要知道的确切信息??关于:$result=$statement->fetchColumn();然后返回$result?我不是作为答案发布,因为我不是100%确定。它将返回所选行数。
return $statement->fetchColumn();