tags:

views:

63

answers:

2

I have a table qotwVote1a with fields QuestionId, MemberId, Vote1a where QuestionId and MemberId are the primary key.

I want to run a query to find that a member, has voted for a question or not. If not, I set the variable $j to 0, if yes I set it to 1.

I wrote this query. But it is giving me an error. Can someone have a look at it? And if you want to ask anything more, let me know.

$questionId=57;
$id="zee";
$result2 = mysql_query("
             SELECT MemberId
             FROM qotwVote1a
             WHERE QuestionId='".$questionId."'
             AND MemberId='".$id."'
             AND MemberId NOT IN ('
               SELECT MemberId 
               FROM qotwVote1a
               WHERE QuestionId='".$questionId."' 
            ')
          ");//WHERE QuestionId='".$questionId."' 
$j=0;
echo $result2;
while($row2 = mysql_fetch_array($result2))
{   
  echo $row2['Vote1a']." ".$row2['QuestionId']." ".$row2['MemberId']; echo "<br/>";
  $j=1;
}
echo($j);
+5  A: 

You have the subquery

(' SELECT MemberId FROM qotwVote1a WHERE QuestionId='".$questionId."' ')

inside single quotes. Remove them:

SELECT MemberId FROM qotwVote1a WHERE QuestionId='".$questionId."' AND MemberId='".$id."' AND MemberId NOT IN (SELECT MemberId FROM qotwVote1a WHERE QuestionId='".$questionId."')
despart
+1  A: 

I have a table qotwVote1a with fields QuestionId, MemberId, Vote1a where QuestionId and MemberId are the primary key.

I want to run a query to find that a member, has voted for a question or not. If not, I set the variable $j to 0, if yes I set it to 1.

I'd do this with a loop over the Members and do the following sql statement for each member:

// assuming to loop over all Members.
$j = 0;

$sql = 'SELECT MemberId'
     . '  FROM qotwVote1a'
     . ' WHERE QuestionId=' . $questionId . '
     . '   AND MemberId=' . $id . '
     . ' LIMIT 1;';    // returns max. 1 row if found else FALSE.

$result2 = mysql_query($sql);
if (FALSE !== ($row2 = mysql_fetch_array($result2))) {
    $j = 1;
}
capfu