简体   繁体   English

在mysql中的行中显示相同的值并打印列值,并且在php中计数

[英]Display same values in rows in mysql and print column value and it's count in php

I want to make a voting list kind of thing. 我想做一个投票表。 I have a form that saves the entered data to mysql database. 我有一个将输入的数据保存到mysql数据库的表格。 Suppose a user entered "Something". 假设用户输入了“某事”。 It'll be saved in column called "bname". 它将保存在名为“ bname”的列中。 Now what I want is Suppose 10 people entered "Something" or "something"(case sensitivity to be ignored automatically if there is a way?) then I want: "Something - 10" to be printed in php page. 现在我想要的是,假设有10个人输入“ Something”或“ something”(如果有办法,区分大小写会自动被忽略吗?)然后我想要:“ Something-10”被打印在php页面中。

My try: 我的尝试:

$query="SELECT bname, count 
FROM brandnames 
GROUP BY bname 
HAVING COUNT(*) > 0
LIMIT 11";

$result=mysqli_query($con,$query) or die('Error!: ' . mysqli_error($con));

$query2="SELECT count
FROM brandnames
GROUP BY count
HAVING COUNT(*) > 0
ORDER BY count DESC
LIMIT 11";

$result2=mysqli_query($con,$query2) or die('Counting Error!: ' . mysqli_error($con));

while ($row=mysqli_fetch_assoc($result))
{
while ($row2=mysqli_fetch_assoc($result2))
{
echo ($row['bname'] . '&nbsp' . '-' . '&nbsp' . $row2['count'] . '<br />');
}
}

This thing prints Something - 4 Something - 3 这东西打印出-4-3

I do not want to use this method. 我不想使用这种方法。 I posted this as if I don't post someone will point me out to post. 我发布了此内容,好像我不发布某人会指出我要发布。 :/ :/

I don't want to use "count" column. 我不想使用“计数”列。 I want a simple voting list as I mentioned above. 我想要一个如上所述的简单投票表。

Thanks, :) 谢谢, :)

The query your looking for should be something like 您要查询的查询应类似于

select bname, count(*) as nbr
  from brandnames
 group by bname
 order by nbr desc
limit 11;

count(*) will return the number of rows that was grouped together by bname. count(*)将返回按bname分组在一起的行数。 Case sensitivity is depending on your coalition. 区分大小写取决于您的联盟。 If it's a case sensitive coaltion you need to group by lower(bname) instead. 如果是区分大小写的联盟,则需要按Lower(bname)分组。 Be aware thou that this might prevent indexes from working for this query. 请注意,这可能会阻止索引对该查询起作用。

Thanks all of you. 谢谢大家。 I got to know how to do this. 我知道如何做到这一点。 Hope this helps someone :) Special thanks to @Andreas Wederbrand 希望这对某人有帮助:)特别感谢@Andreas Wederbrand

<?php

//Create Connection

$query="SELECT columnname, count(*)
AS alias
FROM tablename
GROUP BY columnname
ORDER BY alias DESC";

$result=mysqli_query($con,$query) or die('Counting Error!: ' . mysqli_error($con));



while ($row=mysqli_fetch_array($result))
{
echo ($row['columnname'] . '&nbsp' . '-' . '&nbsp' . $row['alias'] . '<br />');
}

//Close Connection
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM