0

I have attached the screenshot of my output. I guess, the syntax is wrong. Please help.!

<?php
//initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='db';  // Name of the database
$dbUser='root'; // Database username
$dbPass=''; // Database password
 
 
$source = mysql_connect($dbHost, $dbUser, $dbPass);
 
if (!$source) {
	die('Not connected : ' . mysql_error());
}
 
$db_selected = mysql_select_db('db', $source);
 
if (!$db_selected) {
	die ('Can\'t use $DB : ' . mysql_error());
}
 
  //get the information from login form

  $opt=$_POST['options'];
 
 
  //execute the query
  
$i=0;
  if($opt=='Andhra Pradesh')

  {
 
	$sql ="SELECT SUM(v_bjp),SUM(v_cng),SUM(v_mxt) FROM poll_status WHERE state='Andhra Pradesh'";
	
	$result = mysql_query($sql);
  	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);

	$sql ="SELECT SUM(v_bjp),SUM(v_cng),SUM(v_mxt) FROM poll_status WHERE state='Andhra Pradesh'";
	
	$result = mysql_query($sql);
  	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	echo "<b><br><br><center><h1>The poll status of the required state is:</h1><br><br><br><br><h2>"."Votes for BJP : ".$row['SUM(v_bjp)']."</h2><br>";
	
	echo "<h2>"."Votes for Congress : ".$row['SUM(v_cng)']."</h2><br>";
	
	echo "<h2>"."Votes for CPI(M) : ".$row['SUM(v_mxt)']."</h2></b><br>";
  
  	$query1="select * from poll_status";

	$result1=mysql_query($query1);

	$numfields=mysql_num_fields($result1);

	for($i=6;$i < $numfields; $i++)

	{
	$query1="select * from poll_status";

	$result1=mysql_query($query1);

	$numfields=mysql_num_fields($result1);

	$var=mysql_field_name($result1,$i);

	$sql1="SELECT SUM($var) FROM poll_status WHERE state='Andhra Pradesh'";

	$result2=mysql_query($sql1);

	$row1 = mysql_fetch_array($result2, MYSQL_ASSOC);

	echo "<h2>"."Votes for $var : ".$row1['SUM($var)']."</h2></b><br>";
	
	}
			mysql_free_result($result);
				mysql_free_result($result1);
					mysql_free_result($result2);		
	}   
	else if($opt=='Tamil Nadu')

  {
 
	$sql ="SELECT SUM(v_bjp),SUM(v_cng),SUM(v_mxt) FROM poll_status WHERE state='Tamil Nadu'";
	
	$result = mysql_query($sql);
  	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);

	$sql ="SELECT SUM(v_bjp),SUM(v_cng),SUM(v_mxt) FROM poll_status WHERE state='Tamil Nadu'";
	
	$result = mysql_query($sql);
  	
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	
	echo "<b><br><br><center><h1>The poll status of the required state is:</h1><br><br><br><br><h2>"."Votes for BJP : ".$row['SUM(v_bjp)']."</h2><br>";
	
	echo "<h2>"."Votes for Congress : ".$row['SUM(v_cng)']."</h2><br>";
	
	echo "<h2>"."Votes for CPI(M) : ".$row['SUM(v_mxt)']."</h2></b><br>";
  
  	$query1="select * from poll_status";

	$result1=mysql_query($query1);

	$numfields=mysql_num_fields($result1);

	for($i=6;$i < $numfields; $i++)

	{
	$query1="select * from poll_status";

	$result1=mysql_query($query1);

	$numfields=mysql_num_fields($result1);

	$var=mysql_field_name($result1,$i);

	$sql1="SELECT SUM($var) FROM poll_status WHERE state='Tamil Nadu'";

	$result2=mysql_query($sql1);

	$row1 = mysql_fetch_array($result2, MYSQL_ASSOC);

	echo "<h2>"."Votes for $var : ".$row1['SUM($var)']."</h2></b><br>";
	
	}
			mysql_free_result($result);
				mysql_free_result($result1);
					mysql_free_result($result2);
	}   



  echo "<center><br><br><br><br><br><a href=\"http://localhost/project/Loginmodule/adminmenu.html\">Back";

    mysql_close($source);

?>
Attachments screenshot.png 115.74 KB
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by diafol
0

Use an alias with 'AS'.

$sql1="SELECT SUM($var) AS sv FROM poll_status WHERE state='Andhra Pradesh'";
 
	$result2=mysql_query($sql1);
 
	$row1 = mysql_fetch_array($result2, MYSQL_ASSOC);
 
	echo "<h2>"."Votes for $var : ".$row1['sv']."</h2></b><br>";
 
	}

Although position '0' as explained by adkhan works too. However, if you have a number of fields and change the table structure, 'position' soon leads to problems. Much easier to use associative arrays and aliases.

0

great, mark as solved please. The link below the box.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.