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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

    $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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.