[英]PHP MySQL Display counts
I have this query for counting the number of items in a category:我有这个查询来计算一个类别中的项目数:
SELECT category, COUNT(*) AS category_count FROM users GROUP BY category
Which creates results looking like:这会产生如下所示的结果:
category category_count
========== ================
X 3
Y 2
Now, In PHP I want to display the counts of the categories.现在,在 PHP 中我想显示类别的计数。 For example, I might want to echo the count from category X, how would I do it?例如,我可能想从类别 X 中回显计数,我该怎么做?
Thanks in advance提前致谢
Assuming $result
holds the result of your query:假设$result
保存您的查询结果:
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'];
if ($row['category'] == 'X')
{
echo ' Count: ' . $row['category_count'];
}
echo '<br/>';
}
$res = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while($row = mysql_fetch_assoc($res)){
echo $row['category'].": ".$row['category_count']."<br/>";
}
$result = mysql_query("SELECT category, COUNT(*) AS category_count FROM users GROUP BY category");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
{
if ( $row['category'] == 'x' )
{
echo $row['category_count'];
}
}
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'] . ' Count:' . $row['category_count'];
echo "<br>";
}
$conn = mysql_connect("address","login","pas s");
mysql_select_db("database", $conn);
$Var = mysql_query("query");
While ($row = mysql_fetch_assoc($var) { echo $var["column"] }.
如果您在查询中使用 where 子句会更好。
SELECT COUNT(*) AS category_count FROM users WHERE category = 'x'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.