PHP echo SQL Count

时间:2013-05-20 14:17:24

标签: php mysql sql

好的,所以我遇到了问题。我似乎无法在PHP中成功回显SQL Count。

SQL:

SELECT TableA.C, COUNT(*) FROM TableA JOIN TableB ON (TableA.C = TableB.D) 
   WHERE TableB.E = 1 GROUP BY TableA.C ORDER BY COUNT(*) DESC

PHP:

$result= mysql_query("SELECT TableA.C, COUNT(*) FROM TableA JOIN TableB ON (TableA.C = TableB.D) 
   WHERE TableB.E = 1 GROUP BY TableA.C ORDER BY COUNT(*) DESC");

while($rows = mysql_fetch_array($result))
{
     echo $rows['Count']."</br>";
}
$rows = mysql_fetch_array($result);
{
     echo $rows['Count'];
}

我尝试了两种我在网上找到的不同的东西(上面)。我甚至尝试使用“mysql_fetch_array($ result,MYSQL_ASSOC)”而不是mysql_fetch_array($ result)。

每次都会收到相同的错误消息:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in 

/home/semsemx1/public_html/x/xx.php

此外,我已尝试将其设为“$rows['COUNT']”,但这不起作用。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:6)

您只需要将您的点数计算为ALIAS

SELECT TableA.C, COUNT(*) as total

然后你可以用

来调用它
echo $rows['total']

然后我希望您记住mysql_*函数已被弃用,因此我建议您切换为mysqliPDO

答案 1 :(得分:3)

使用“As”关键字

SELECT TableA.C, COUNT(*) as count FROM TableA JOIN TableB ON (TableA.C = TableB.D) WHERE TableB.E = 1 GROUP BY TableA.C ORDER BY COUNT(*) DESC


 echo $rows['count '];