比较3个变量并回应最多投票的赢家

时间:2016-12-08 17:54:00

标签: php html css mysql

我对MySQL数据库有3个查询,但我会像这个例子一样简单:

$var1 = "354";
$var2 = "980";
$var3 = "27";

我需要以某种方式比较这3个变量,并且只显示最大的 - 在本例中,$ var2的值为980。

为此,我运行:

$biggest = max($var1, $var2, $var3);

现在我需要显示以下echo示例:

the variable $var2 is the winner with 980 votes

如何实现?

这是分配变量值的方式:

$var1=mysqli_fetch_assoc(mysqli_query($datacenter, 
"SELECT COUNT(comite) FROM votos WHERE `comite` = '1'"));
$var2=mysqli_fetch_assoc(mysqli_query($datacenter, 
"SELECT COUNT(comite) FROM votos WHERE `comite` = '2'"));
$var3=mysqli_fetch_assoc(mysqli_query($datacenter, 
"SELECT COUNT(comite) FROM votos WHERE `comite` = '3'"));

1 个答案:

答案 0 :(得分:2)

使用完全不同的方法实际上可能会更好 - 让mySQL做繁重的工作:

$result = mysqli_query($datacenter, "SELECT `comite`, COUNT(`comite`) `count` FROM `votos` GROUP BY `comite` ORDER BY COUNT(`comite`) DESC LIMIT 0, 1");
$row = mysqli_fetch_assoc($result);
echo "The winner is ".$row['comite']." with ".$row['count']." votes";