在PHP中显示数据的行数

时间:2018-04-12 18:16:07

标签: php mysqli

已经过了一段时间......这实际上是我之前发布的问题的另一个简化问题。我试图显示国家/地区并计入以下查询:

$query = mysqli_query($con, "SELECT COUNT(id), country FROM users GROUP BY country ORDER BY COUNT(id) DESC");

    while ($row = mysqli_fetch_array($query)) {
    $countries = $row['country'];
    echo $countries;}

我在输出中获得的是从最高到最低列出的国家/地区,但它旁边没有显示计数。我知道这些已在查询中总结,我如何定位数字&将它分配给php变量进行显示?

任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

您可以为COUNT列使用别名,然后在mysqli_fetch_array为每行返回的数组中引用该别名。

$query = mysqli_query($con, "SELECT COUNT(id) AS countryCount, country FROM users GROUP BY countryCount ORDER BY countryCount DESC");

while ($row = mysqli_fetch_array($query)) {
$count = $row['countryCount'];
$countries = $row['country'];
echo $count;
echo $countries;}

答案 1 :(得分:0)

您需要在COUNT(id)的查询中添加别名,如下所示: -

$query = mysqli_query($con, "SELECT COUNT(id) as count, country FROM users GROUP BY country ORDER BY count DESC");

现在更改while()循环代码: -

while ($row = mysqli_fetch_assoc($query)) { // _assoc for lighter array iteration
    $countries = $row['country'];
    $countries_count = $row['count'];// get count of each country
    echo $countries.'--'.$countries_count ;// show country and it's count both 
}

答案 2 :(得分:0)

这是你想要做的: -

$query = mysqli_query($con, "SELECT COUNT(id) AS total_country, country FROM users GROUP BY country ORDER BY id DESC");

while ( $row = mysqli_fetch_array( $query ) )
{
    $countries           = $row['country'];
    $total_countries = $row['total_country'];

    echo "<p>{$countries}</p>;
    echo "<p>Total count: {$total_countries}</p>";
}

答案 3 :(得分:-4)

您没有分配变量。

执行此操作:In [1]: letters = set(['F','I', 'I', 'X', 'O', 'R', 'E']) In [2]: find = set(['F', 'I', 'X']) In [3]: find.issubset(letters) Out[3]: True In [4]: find <= letters Out[4]: True 并回复它。它应该工作