我有3个查询到MySQL数据库,但我会像下面的示例一样简单:
$var1 = "354";
$var2 = "980";
$var3 = "27";
我需要以某种方式比较这三个变量并仅显示最大的变量,在此示例中,值为$ 980的$ var2。
为此,我运行:
$biggest = max($var1, $var2, $var3);
现在,我需要显示以下回显示例:
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'"));
最佳答案
使用完全不同的方法实际上可能会更好-让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";