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);

?>

Try using $row1[0] instead of $row1

Member Avatar for diafol

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.

Hey thanks! That worked perfectly. :)

Member Avatar for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.