Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算大于20的值的数量_Php_Mysql_Sql_Pdo - Fatal编程技术网

Php 计算大于20的值的数量

Php 计算大于20的值的数量,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,我的数据库中有一个表,tbl。在该表中,id和num都是常规的int值。 我想计算有多少ID的num大于20(num>20)。只是为了计算有多少行num>20。我写道: $counter= 0; $sqlQuery = "select num from tbl"; $finalResult= $databasename->prepare($sqlQuery ); $finalResult->execute(); $numArr= $finalResult->fetchColum

我的数据库中有一个表,tbl。在该表中,id和num都是常规的int值。
我想计算有多少ID的num大于20(num>20)。只是为了计算有多少行num>20。我写道:

$counter= 0;
$sqlQuery = "select num from tbl";
$finalResult= $databasename->prepare($sqlQuery );
$finalResult->execute();
$numArr= $finalResult->fetchColumn();
foreach ($numArra $row){
    if($row > 20)
        $counter++;
}

echo ($counter);

问题是,它每次打印0。。。提前谢谢。

您不需要这些。照办

SELECT COUNT(*) FROM tbl WHERE num > 20
如果您希望将其插入PHP,并且希望动态地执行此操作

$finalResult= $databasename->prepare("SELECT COUNT(*) FROM tbl WHERE num > ?");
$finalResult->bindParam(1,$someParam);
$finalResult->execute();
$numArr = $finalResult->fetchColumn();

echo ($numArr);

更简单

这样做很好&更方便、更简单。非常感谢你!还有一件事——如果我想输入一个值,而不是仅仅写“20”?我怎么能做到呢?因为我是新来的,所以不能投票,但我肯定接受了答案。再次感谢!