akshay akshay - 9 months ago 39
MySQL Question

How to retrieve the highest voted candidate name with number of votes from table

This is my php code of webpage. I want to retrieve the candidate's name as well as number of votes. But I don't know how to. As you can see I have tried a lot but no use. My database name is election, my table name is votes. In table I have 3 columns id, name, votes, and only three candidate are inserted (not more than that).

<script type="text/javascript" src="http://localhost/WTL/js/validateLogin.js">
<style type="text/css">

border:solid 2px #B22222;



<div class="header">
<img src="http://localhost/WTL/images/home2.jpg" id="home" width="1350" height="180">
<div name="header2">
<form name="result" method="post" action="">
<Table class="tabb" align="center" >
<font size="5px" color="#B22222">RESULT</font>

<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>
<center><div name="header3" class="last"><input type="submit" name="sub" value="RESULT" onclick="validate();" class="btn" ></div></center>
$display= "";

$db = mysqli_connect("localhost","root","","election");
die("connection failed".$db->connect_error);

$sql="SELECT name,votes FROM cr
WHERE votes = (SELECT Max(votes) FROM cr)";
echo '<textarea>', $res , '</textarea>';
//$display .= '<div>'.$name.' '.$votes.'</div>';

Answer Source

The original question contain many mistakes which mostly seems to have segnalated by the comments to the question and other answers this response is limited to only part of the code which relates to the display of results

for get the result you should use a loop (assuming that you have not errors) for access to each row array and proper index for get the each column

 while ($row = mysqli_fetch_array($res))

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