php - 从数据库中查找数据的计数

标签 php mysql sql mysqli count

我想从数据库中找到某些项目的数量。我使用了这段代码

$sql=" SELECT count(*) from request WHERE status = '0'";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0)
    {
        while($row = mysqli_fetch_assoc($result))
            {
                echo "<pre>";
                    print_r($row);
                echo "</pre>";

            }
    }

我在一行中得到这个数组

Array
(
    [count(1)] => 1
)

为了从这个数组中获取值,我使用了

$total = $row[0];
echo $total; 

但没有得到任何结果。我怎样才能从这个数组中获取值

最佳答案

你需要使用:

$row = mysqli_fetch_row($result);
echo $row[0];

或将您的查询更改为:

$sql=" SELECT count(*) as `num` from request WHERE status = '0'";

并使用:

$row = mysqli_fetch_assoc($result));
echo $row['num'];

关于php - 从数据库中查找数据的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31132091/

相关文章:

sql - 在 BigQuery Google Analytics 数据中提取两个页面之间的用户旅程数据

python ( Pandas ): store a data frame in hdf5 with a multi index

javascript - Bootstrap 的 JavaScript 在 vue 2.5 中需要 jQuery

java - 阻止网站自动访问

php - 如何在 WordPress 网站中找到损坏的 php 或 WP 文件

java - 如何修复 "tables are already present using metadata"camunda进程引擎异常?

sql - LIKE 匹配以数字结尾的字符串

php - 停止处理并评估 foreach 循环中的下一个值

php - Laravel 迁移的好处

python - Alembic 是否可以应用于现有数据库,并在检测到表/列已存在时跳过创建/更改表/列?