无法按查询计算行

时间:2019-06-22 07:23:30

标签: php mysql

无法根据ID对查询结果进行计数

$userid =$_SESSION['userid'];   
$sql="SELECT id FROM midm WHERE userid='$userid'";
$mID=mysql_query($sql);
$query ="SELECT forwarded,COUNT(forwarded) FROM report WHERE 
id='$mID' AND forwarded='Not seen'";
$result = mysql_query($query) or die (mysql_error());
while ($row=mysql_fetch_array($result)){
echo "[".$row['COUNT(forwarded)']."]";  
}?> </a>

我的预期输出是计数所有未看到的取决于id。任何可以帮助我的人..对不起我对我的问题的描述很抱歉,您可以对其进行编辑。

1 个答案:

答案 0 :(得分:-1)

尝试将查询更改为

SELECT COUNT(forwarded) AS forwardCount FROM report WHERE id='$mID' AND forwarded='Not seen'"

因为您要查找特定的“转发”值,所以不需要将其包含在from中。同样,“ AS forwardCount”为您提供了一个数组别名以供使用,因此将回显更改为

echo "[".$row['forwardCount']."]";

修改

如果要对多个值进行计数,请在末尾添加GROUP BY。

SELECT forwarded, COUNT(forwarded) AS forwardCount FROM report WHERE id='$mID' AND forwarded='Not seen' GROUP BY forwarded

而且,正如Strawberry和Dharman所提到的,至少使用mysqli_库而不是mysql _