2016-11-20 138 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

相关问题