PHP echo SQL计数 [英] PHP echo SQL Count
问题描述
好的,所以我有问题.我似乎无法在PHP中成功回显SQL计数.
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']
",但这不起作用.
任何帮助将不胜感激.
您只需要给计数加ALIAS
SELECT TableA.C, COUNT(*) as total
然后您可以使用
进行调用echo $rows['total']
然后,我希望您记住不推荐使用mysql_*
函数,因此我建议您切换到 mysqli
或 PDO
>
Okay, so I am having a problem. I seem to be unable to successfully echo an SQL Count in PHP.
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'];
}
I've tried two different things I've found online (the above). I even tried one with "mysql_fetch_array($result, MYSQL_ASSOC)" instead of just mysql_fetch_array($result).
Each time, I get the same error messages:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in
/home/semsemx1/public_html/x/xx.php
Additionally, I've tried capitalizing as "$rows['COUNT']
", but that doesn't work.
Any help would be appreciated.
You just need to give your count an ALIAS
SELECT TableA.C, COUNT(*) as total
then you can call it with
echo $rows['total']
Then I would like you to remember that mysql_*
functions are deprecated so i would advise you to switch to mysqli
or PDO
这篇关于PHP echo SQL计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!