Jump to content

Why the code (below) is not working ?...pls help...


jd2007

Recommended Posts

<?php
/*
session_start();
if (!isset($_SESSION['Logged_In'])) {
    echo "Please click here and login.";
    exit;
}
else {
  
    $db = mysql_connect("localhost", "root");
    $connection=mysql_select_db("Voters", $db);
    $query="SELECT * FROM Voters1 WHERE nickname=($_SESSION['nickname'])";
    $result=mysql_query($query);
    if (mysql_num_rows($result) == 0) {
	echo "There is no such nickname $_SESSION['nickname']. ";
echo "To vote, please register and log in.";
	exit;
      }
     $_SESSION['nickname']
    
}  */
if (isset($_POST["vote"])) {
    if ($_POST["NextPresident"]=="Barack Obama") {
    $db2 = mysql_connect("localhost", "root");
    $connection2=mysql_select_db("VoteResults", $db2);
    $querya="INSERT INTO NP1 (Obama)
    VALUES (1)";
    $resulta=mysql_query($querya);
   
     $query2="SELECT Obama FROM NP1";
     $result2=mysql_query($query2);
     $rownum=mysql_num_rows($result2);
     if ($rownum == 0) {
	echo "No results to display yet.";
	exit;
      }
      
      while ($record = mysql_fetch_row($result2)) {
	while (list($fieldname, $fieldvalue) = each ($record)) {
	        //for (i=0; i<$rownum; i++) {
	         if ($fieldvalue=="NULL") {
	         $rownum--;
	         }
	         else {
	         $rownum;
	         }
	        //}
	}
      }
     $query3="SELECT Clinton FROM NP1";
     $result3=mysql_query($query3);
     $rownum2=mysql_num_rows($result3);
     if ($rownum2 == 0) {
	echo "No results to display yet.";
	exit;
      }
      
      while ($record2 = mysql_fetch_row($result3)) {
	while (list($fieldname2, $fieldvalue2) = each ($record2)) {
	        //for (i=0; i<$rownum; i++) {
	         if ($fieldvalue=="NULL") {
	         $rownum2--;
	         }
	         else {
	         $rownum2;
	         }
	        //}
	}
      }
      $total=$rownum+$rownum2;
      $obama_perc=($rownum / $total) * 100;
      $clinton_perc=($rownum2 / $total) * 100;
    
    if ($result) {
      echo "<img src='barack.png' width='".$obama_perc."' height='20' alt=''>";
      echo "<br><br>";
      echo "<img src='clinton.png' width='".$clinton_perc."' height='20' alt=''>";
    }
    else {
      echo "There was an error, sorry, please try again later."; 
    }
    }
    else {
      $db2 = mysql_connect("localhost", "root");
    $connection2=mysql_select_db("VoteResults", $db2);
    $querya="INSERT INTO NP1 (Clinton)
    VALUES (1)";
    $resulta=mysql_query($querya);
    
     $query2="SELECT Obama FROM NP1";
     $result2=mysql_query($query2);
     $rownum=mysql_num_rows($result2);
     if ($rownum == 0) {
	echo "No results to display yet.";
	exit;
      }
      
      while ($record = mysql_fetch_row($result2)) {
	while (list($fieldname, $fieldvalue) = each ($record)) {
	        //for (i=0; i<$rownum; i++) {
	         if ($fieldvalue=="NULL") {
	         $rownum--;
	         }
	         else {
	         $rownum;
	         }
	        //}
	}
      }
     $query3="SELECT Clinton FROM NP1";
     $result3=mysql_query($query3);
     $rownum2=mysql_num_rows($result3);
     if ($rownum2 == 0) {
	echo "No results to display yet.";
	exit;
      }
      
      while ($record2 = mysql_fetch_row($result3)) {
	while (list($fieldname2, $fieldvalue2) = each ($record2)) {
	        //for (i=0; i<$rownum; i++) {
	         if ($fieldvalue=="NULL") {
	         $rownum2--;
	         }
	         else {
	         $rownum2;
	         }
	        //}
	}
      }
      $total=$rownum+$rownum2;
      $obama_perc=($rownum / $total) * 100;
      $clinton_perc=($rownum2 / $total) * 100;
    
    if ($result) {
      echo "<img src='barack.png' width='".$obama_perc."' height='20' alt=''>";
      echo "<br><br>";
      echo "<img src='clinton.png' width='".$clinton_perc."' height='20' alt=''>";
    }
    else {
      echo "There was an error, sorry, please try again later."; 
    }
    }
}

else {
   echo "You must vote first!";
}

?>

 

 

the output is like this :

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\Voting\voting2.php on line 92

No results to display yet.

<?php $result2=mysql_query($query2); ?>

 

there's your problem. mysql_query takes two parameters, the query, and then the connection.

 

it should be:

<?php $result2=mysql_query($query2, $db2); ?>

 

that should work. you have this more than once.  every time you have a query i see this mistake.

    $connection2=mysql_select_db("VoteResults", $db2);

 

make that just

 

  mysql_select_db("VoteResults",$db2);

 

Then you don't have to change all your queries.

 

NOTE

 

This is useful when you're only using ONE connection throughout the script - if you're connecting to multiple DBs it's better to do it as daled mentions

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.