Richard_Hollenbeck Posted November 22, 2013 Share Posted November 22, 2013 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.) Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/ Share on other sites More sharing options...
Solution kicken Posted November 22, 2013 Solution Share Posted November 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/#findComment-1459466 Share on other sites More sharing options...
Richard_Hollenbeck Posted November 22, 2013 Author Share Posted November 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/#findComment-1459467 Share on other sites More sharing options...
Richard_Hollenbeck Posted November 22, 2013 Author Share Posted November 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/#findComment-1459468 Share on other sites More sharing options...
Richard_Hollenbeck Posted November 22, 2013 Author Share Posted November 22, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/#findComment-1459469 Share on other sites More sharing options...
Richard_Hollenbeck Posted November 23, 2013 Author Share Posted November 23, 2013 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: 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. Quote Link to comment https://forums.phpfreaks.com/topic/284146-incorrect-comparison-logical-error-for-sure/#findComment-1459641 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.