Jump to content

Incorrect Comparison (logical error for sure)


Go to solution Solved by kicken,

Recommended Posts

I am temporarily including some extra "testing" code within my program to help me troubleshoot it.  It won't be in the final program.  I'm trying to compare the correct answer in a quiz program ($ans) with the user's answer ($_POST['guess']).  Even when the user picks the right answer, the program still spits out, "wrong!"  I am not seeing my logical error here, but I obviously have one.  I attached a screen shot of the output.  I hope it helps.  Thank you all.

if (isset($_POST['qid'])){ //qid is the question id
     $_SESSION['qid']=$_POST['qid'];
    print_r($_SESSION);

$ans = $db->query("SELECT answer FROM QuestionsAnswers WHERE questionid = " .  $_SESSION['qid']);

    echo "<br />";
    print_r ($ans);  //correct answer

while($row = $ans->fetch(PDO::FETCH_ASSOC)) {
    echo "<p>Here is my Thursday 6pm attempt:xxx" . intval($row['answer']) . "xxx</p>";
    echo "<p>User guessed:xxx" . intval($_POST['guess']) . "xxx</p>";
    echo '<p>the extra exes ("xxx") are to demonstrate there are no spaces in the field data.</p>';
}

if (intval($row['answer']) == intval($_POST['guess'])){ //values converted to integers just in case one or both is/are a string
    echo "<p>Right!</p>";}
  else{
    echo "<p>wrong!</p>";
}

It is bugging me that PHPFreaks won't let me log into my regular account.  Cool.  At least I can log in using Facebook.  Different account, same person.  It is still me.  Thanks to all for any help with this one.
 

(please see attached output.)

 

post-166180-0-94123200-1385090618_thumb.png

  • Solution

Where your if statement is, $row is going to be equal to false, since that is the only way to get out of your while loop.

 

If there is only going to be one row returned from your query, then just drop the while loop entirely and do $row = $ans->fetch(PDO::FETCH_ASSOC);. If there might be multiple rows then you need to move the if condition inside the loop body.

I must  have deleted a right curly brace (}) from the code when I pasted it here because I don't see it in the code.  It is in the actual code.  It would not have produced any output if it wre missing.

 

Here's a snipped of the code with the curly brace still where it actually still is:

// ...
if (isset($_POST['guess'])){
     $_SESSION['guess']=$_POST['guess'];

} // missing curly brace is still there.  It accidentally got deleted from my example.

if (isset($_POST['qid'])){
     $_SESSION['qid']=$_POST['qid'];
    print_r($_SESSION);
 // ...

Sorry for the omission.  That was NOT the error I was writing about, but it WAS an error to fail to include it in the code I posted.

Where your if statement is, $row is going to be equal to false, since that is the only way to get out of your while loop.

 

If there is only going to be one row returned from your query, then just drop the while loop entirely and do $row = $ans->fetch(PDO::FETCH_ASSOC);. If there might be multiple rows then you need to move the if condition inside the loop body.

 

Thank you. 

 

However I caught your point just as you were sending it.  It is not like that in the actual code.  The if condition is outside the loop.  I accidentally omitted a right curly brace in the code I posted (sorry). 

 

The problem still exists.

Yep!  you were right.  My missing bracket in my example here wasn't the problem, but you were right about the condition never evaluating to true the way my statement was outside the loop. I moved the condition inside the loop.  Great. Thank you.

Thanks.  I was wondering about that.  Why run a loop when I am going directly to the only record I need?  Great.

 

kicken, on 21 Nov 2013 - 8:56 PM, said:snapback.png

If there is only going to be one row returned from your query, then just drop the while loop entirely and do $row = $ans->fetch(PDO::FETCH_ASSOC);. If there might be multiple rows then you need to move the if condition inside the loop body.

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.