我有这个查询来计算一个类别中的项目数:
SELECT category, COUNT(*) AS category_count FROM users GROUP BY category
这会产生如下结果:
category category_count
========== ================
X 3
Y 2
现在,在PHP中我想显示类别的计数。例如,我可能想要回应X类中的计数,我该怎么做?
提前致谢
答案 0 :(得分:2)
假设$result
包含您的查询结果:
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'];
if ($row['category'] == 'X')
{
echo ' Count: ' . $row['category_count'];
}
echo '<br/>';
}
答案 1 :(得分:0)
$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/>";
}
答案 2 :(得分:0)
$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'];
}
}
答案 3 :(得分:0)
while ($row = mysql_fetch_array($result))
{
echo 'Category: ' . $row['category'] . ' Count:' . $row['category_count'];
echo "<br>";
}
答案 4 :(得分:0)
$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"] }.
答案 5 :(得分:0)
如果在查询中使用where子句会更好。
SELECT COUNT(*) AS category_count FROM users WHERE category = 'x'