2016-11-20 142 views
-3

這是我的網頁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>'; 
} 
} 
?> 
+1

*「幫助!它的緊急」 * - 原來如此。順便說一句,你不能使用「回聲」,'$ res'查詢。 –

+0

是我知道,但我嘗試因爲我不知道該怎麼辦 – akshay

+1

爲什麼你不只是通過限制1的投票降序? '選擇名稱,投票從CR ORDER BY投票DESC LIMIT 1' - 更清楚一點。 –

回答

-3

原來的問題包含很多錯誤,其中大部分似乎已經被評論的問題等回答了這個響應僅限於這關係到顯示的代碼只是一部分segnalated結果

爲得到你應該使用一個循環的結果(假設你有沒有錯誤)訪問每排陣和適當的索引獲得每列

while ($row = mysqli_fetch_array($res)) 
    { 

     echo '<textarea>'. $row['name'] .'</textarea><br/>'; 
     echo '<textarea>'. $row['votes'] .'</textarea><br/>'; 
    } 
+0

@MagnusEriksson ..正確..(我更喜歡點連接) – scaisEdge

+0

正如我在評論中所述,他們的代碼中還存在其他很多錯誤。 –

+0

@ Fred-ii-不僅有幾個......看起來很多..我只提出了主要問題..(由我) – scaisEdge

-3

您的sql語法有錯誤。您的代碼應該是這樣的:

<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 
$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, Max(votes) as vote FROM cr group by name"; 
    $res=mysqli_query($db,$sql); 
    //$name=$res['name']; 
    //$votes=$res['votes']; 
    echo '<textarea>'.$res['name'].' - '.$res['vote'].'</textarea>'; 
    //$display .= '<div>'.$name.' '.$votes.'</div>'; 
} 
} 
?> 
+0

它說不能使用mysqli_result類型的對象作爲數組在blah等等... – akshay

+0

他的SQL不是問題。你的例子不會工作,因爲'mysqli_query()'不返回一個數組... –

+0

得到了輸出... $ sql =「SELECT name,votes FROM cr WHERE votes =(SELECT Max(votes)FROM CR)「; \t $ res = mysqli_query($ db,$ sql); ($ row = mysqli_fetch_array($ res)) \t { echo'
'; echo'
'; } – akshay

相關問題