這是我的網頁php代碼。我想要檢索候選人的姓名以及投票數。但我不知道如何。正如你可以看到我已經嘗試了很多,但沒有用。我的數據庫名稱是選舉,我的表名是票。在表中,我有3列id,名稱,投票,只有三名候選人被插入(不超過)。如何從表格中檢索票數最高的候選人名稱
<html>
<head>
<title>RESULT</title>
<script type="text/javascript" src="http://localhost/WTL/js/validateLogin.js">
</script>
<style type="text/css">
.btn{
border-radius:15px;
border:solid 2px #B22222;
padding:5px;
width:10%;
height:8%;
background-color:white;
float:center;
}
</style>
</head>
<body>
<div class="header">
<img src="http://localhost/WTL/images/home2.jpg" id="home" width="1350" height="180">
</div>
<div name="header2">
<form name="result" method="post" action="">
<Table class="tabb" align="center" >
<th>
<font size="5px" color="#B22222">RESULT</font>
</th>
<tr><td><input type="textarea" rows="6" cols="50" width="50%" height="50%" name="resultDisplay" placeholder="click result to check the result" class="textarea"></td></tr>
</table>
<center><div name="header3" class="last"><input type="submit" name="sub" value="RESULT" onclick="validate();" class="btn" ></div></center>
</form>
</div>
</body>
</html>
<?php
session_start();
$display= "";
if(isset($_REQUEST['sub']))
{
$db = mysqli_connect("localhost","root","","election");
if($db->connect_error)
{
die("connection failed".$db->connect_error);
}
else
{
$sql="SELECT name,votes FROM cr
WHERE votes = (SELECT Max(votes) FROM cr)";
$res=mysqli_query($db,$sql);
//$name=$res['name'];
//$votes=$res['votes'];
echo '<textarea>', $res , '</textarea>';
//$display .= '<div>'.$name.' '.$votes.'</div>';
}
}
?>
*「幫助!它的緊急」 * - 原來如此。順便說一句,你不能使用「回聲」,'$ res'查詢。 –
是我知道,但我嘗試因爲我不知道該怎麼辦 – akshay
爲什麼你不只是通過限制1的投票降序? '選擇名稱,投票從CR ORDER BY投票DESC LIMIT 1' - 更清楚一點。 –