Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/74.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 如何计算每所大学的学生人数_Php_Sql_Count_Echo_Distinct - Fatal编程技术网

Php 如何计算每所大学的学生人数

Php 如何计算每所大学的学生人数,php,sql,count,echo,distinct,Php,Sql,Count,Echo,Distinct,我有一个大学生SQL数据库,详细信息如下: Table_name: register Column_names: position, tertinst 数据库中的数据如下所示: Coach..........UCT Athlete........Tukkies Official.......University of JHB Athlete........Tukkies Athlete........Tshwane Tech Ma

我有一个大学生SQL数据库,详细信息如下:

    Table_name: register
    Column_names: position, tertinst
数据库中的数据如下所示:

    Coach..........UCT
    Athlete........Tukkies
    Official.......University of JHB
    Athlete........Tukkies
    Athlete........Tshwane Tech
    Manager........UCT
   UCT.....................735
   University of Jhb.......668
   Tukkies.................886
我需要计算每所大学(tertinst)运动员(职位)的学生人数,输出必须如下所示:

    Coach..........UCT
    Athlete........Tukkies
    Official.......University of JHB
    Athlete........Tukkies
    Athlete........Tshwane Tech
    Manager........UCT
   UCT.....................735
   University of Jhb.......668
   Tukkies.................886
这是我的尝试:

$positionx = 'Athletes';

include_once 'core/includes/db_connect.php';

$sql = "SELECT tertinst, COUNT(position) FROM register WHERE position = '$positionx' GROUP BY tertinst ";

$result = $conn->query($sql);
while ($row = mysql_fetch_array($result)) {
    echo $row['COUNT(tertinst)'] . '......' . $row['COUNT(position)'];
}

$conn->close();

执行代码时,我没有得到任何结果,并且我已经搜索了几个小时不同的解决方案,但没有成功。

我犯了一些错误,并更正了sql计数中的语法以及echo。以下是我的问题的解决方案:

    <?php

include_once 'core/includes/db_connect.php';

$sql = "SELECT tertinst, COUNT(*) total FROM register WHERE position = 'Athletes' GROUP BY tertinst ";
$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
    $tertinst = $row["tertinst"];

    echo $tertinst . '......' . $row['total'] . '<br>';
}
$conn->close();

不是你的问题,但是。。。。使用mysqli函数,而不是不推荐使用的mysql函数。查看准备好的安全声明,尝试获取有用的错误或警告,查看您的错误。logTypo?将while($row=mysql\u fetch\u array($result))替换为
foreach($result as$row)
到底是什么问题?如果是打字错误,你真的认为人们将来会发现这个问题,并且觉得这个解释很有用吗。如果问题只是一个打字错误,你可以删除这个问题。打字错误只是一种犯错误的表达方式。我的答案是web上sql count解决方案的组合,它应该可以帮助任何需要做类似事情的人。您可以清楚地看到sql计数和echo的语法是不同的<代码>谢谢!我会的。