dan_t Posted April 17, 2011 Share Posted April 17, 2011 This is probably a simple question, but if you are looping through a while loop using an if - else statement why would it echo the answer the amount of times there are data fields? Like if you had three users in a database and you had an if statement to validate a user name and password. If the user name and password passed it would say good - if it failed it would say no good. But instead it prints - good no good good, or no good good good, ect.??? Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/ Share on other sites More sharing options...
kenrbnsn Posted April 17, 2011 Share Posted April 17, 2011 Code? Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202496 Share on other sites More sharing options...
dan_t Posted April 17, 2011 Author Share Posted April 17, 2011 This is on of the hundred ways I have tried it: $query = "SELECT username, password FROM userTable"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $logged = (($row['username'] == $username) && ($row['password'] == $password)); if(!$logged){ echo "Sorry"; } else { echo "Good!"; } I also tried a switch statement, but have become frazzled. Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202498 Share on other sites More sharing options...
kenrbnsn Posted April 17, 2011 Share Posted April 17, 2011 What are you trying to accomplish with this code? Ken Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202499 Share on other sites More sharing options...
dan_t Posted April 17, 2011 Author Share Posted April 17, 2011 I did have code sending it to one page or another printing the content and username on the one page, but it printed everything multiple times so now I'm just trying to fix that problem first. Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202500 Share on other sites More sharing options...
kenrbnsn Posted April 17, 2011 Share Posted April 17, 2011 If you only want records where the username & password match those in the DB, do that via the query: <?php $query = "SELECT username, password FROM userTable where username='$username' and password='$password'"; ?> This assume that the values of $username & $password have been properly sanitized. Ken Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202501 Share on other sites More sharing options...
dan_t Posted April 17, 2011 Author Share Posted April 17, 2011 "This assume that the values of $username & $password have been properly sanitized" What do you mean by sanitized? Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202502 Share on other sites More sharing options...
maxudaskin Posted April 17, 2011 Share Posted April 17, 2011 You just have to break the loop once found. Better yet, change your SQL to get the password from the database using the username. Instead of <?php $query = "SELECT username, password FROM userTable"; $result = mysql_query($query); while($row = mysql_fetch_array($result)) { $logged = (($row['username'] == $username) && ($row['password'] == $password)); if(!$logged){ echo "Sorry"; } else { echo "Good!"; } } } Do this: <?php $sql = 'SELECT `password` FROM `userTable` WHERE username = \'' . $username . '\''; $query = mysql_query($sql); if(mysql_num_rows($query) < 1) { // If there are no results found with that username, tell the user echo 'Incorrect Username'; } else { // Use and else statement so that it doesn't give errors when checking the password (no result means no password to check) $result = mysql_result($query); if($result != $password) { echo 'Incorrect Password'; } else { echo 'Login Success!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/233944-whlie-loop/#findComment-1202508 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.